Skip to content

Commit

Permalink
Merge remote-tracking branch 'layerx/BEPRO-2164-points-system' into B…
Browse files Browse the repository at this point in the history
…EPRO-2172-points-system-action-accept-proposal
  • Loading branch information
vhcsilva committed Apr 29, 2024
2 parents e56152b + bca46c3 commit c63c33d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/actions/points-system/calculate-user-points.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Op } from "sequelize";
import db, { sequelizeConnection } from "src/db";
import logger from "src/utils/logger-handler";

export const name = "CalculatePointsDaily";
export const schedule = "0 0 * * *";
export const description = "Calculate total points and update users' total points";
export const author = "Vitor Hugo";

export async function action() {
logger.info(`${name} start`);

const events = await db.points_events.findAll({
where: {
pointsCounted: false,
},
order: ["userId"],
raw: true
});

if (!events?.length) {
logger.info(`${name} no uncounted events found`);
return;
}

const pointsByUser = {} as { [userId: string]: { total: number, eventsIds: number[] } };

for (const event of events) {
if (!pointsByUser[event.userId])
pointsByUser[event.userId] = { total: 0, eventsIds: [] };

pointsByUser[event.userId].total += event.pointsWon;
pointsByUser[event.userId].eventsIds.push(event.id);
}

const updateUserPoints = Object.entries(pointsByUser).map(([id, { total }]) => ({ id, total }));
const parsedEventsIds = Object.values(pointsByUser).map(({ eventsIds }) => eventsIds).flat();

try {
await Promise.all(updateUserPoints.map(item => db.users.update({
totalPoints: item.total
}, {
where: {
id: item.id
}
})));

await db.points_events.update({
pointsCounted: true,
}, {
where: {
id: {
[Op.in]: parsedEventsIds
}
}
});


logger.info(`${name} updated user points`, updateUserPoints);
} catch(error) {
logger.error(`${name} failed to update user points`, updateUserPoints, error);
}
}
4 changes: 2 additions & 2 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ if (NEXT_DB_SSL === "true")
},
};

const con = new Sequelize(
export const sequelizeConnection = new Sequelize(
options.database!,
options.username!,
options.password!,
options
);

const modules = initModels(con);
const modules = initModels(sequelizeConnection);

export default modules;
2 changes: 2 additions & 0 deletions src/modules/chain-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {action as UpdateHeaders} from "../actions/get-prices-header-information"
import {action as GenerateNftImage} from "../actions/generate-nft-images";
import {action as UserLockedAmountChanged} from "../actions/get-user-locked-amount-changed-event";
import {action as ChangedFee} from "../actions/get-changed-fee";
import {action as CalculateUserPoints} from "../actions/points-system/calculate-user-points";

/**
* These events rely on parsed-logs to function and can/will rely on Database information, as well as update it
Expand Down Expand Up @@ -55,6 +56,7 @@ export const MIDNIGHT_ACTIONS = {
DeletePendingNetworks,
DeletePendingBounties,
UpdateNetworkParams,
CalculateUserPoints,
}

export const MINUTE_ACTIONS = {
Expand Down

0 comments on commit c63c33d

Please sign in to comment.