Skip to content

Commit

Permalink
refactor(user tabcoin tabcash): create tables to store user tabcoin a…
Browse files Browse the repository at this point in the history
…nd tabcash operations
  • Loading branch information
Rafatcb committed Mar 22, 2024
1 parent 49921e5 commit be9132c
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 380 deletions.
146 changes: 0 additions & 146 deletions models/balance.js

This file was deleted.

46 changes: 8 additions & 38 deletions models/ban.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import database from 'infra/database.js';
import balance from 'models/balance.js';
import contentTabcoin from 'models/content-tabcoin.js';
import session from 'models/session.js';
import userTabcash from 'models/user-tabcash.js';
import userTabcoin from 'models/user-tabcoin.js';
import user from 'models/user.js';

async function nuke(userId, options = {}) {
Expand Down Expand Up @@ -34,24 +35,13 @@ async function nuke(userId, options = {}) {
const userEvents = await getAllRatingEventsFromUser(userId, options);

for (const userEvent of userEvents) {
const userEventBalanceOperations = await getRatingUserBalanceOperationsFromEvent(userEvent.id, options);
const promises = [
userTabcoin.undoAllByOriginatorId(userEvent.id, options),
userTabcash.undoAllByOriginatorId(userEvent.id, options),
contentTabcoin.undoAllByOriginatorId(userEvent.id, options),
];

const contentEventBalanceOperations = await contentTabcoin.findAll(
{
where: {
originatorId: userEvent.id,
},
},
options,
);

for (const eventBalanceOperation of userEventBalanceOperations) {
await balance.undo(eventBalanceOperation.id, options);
}

for (const eventBalanceOperation of contentEventBalanceOperations) {
await contentTabcoin.undo(eventBalanceOperation.id, options);
}
await Promise.all(promises);
}

async function getAllRatingEventsFromUser(userId, options = {}) {
Expand All @@ -75,26 +65,6 @@ async function nuke(userId, options = {}) {

return results.rows;
}

async function getRatingUserBalanceOperationsFromEvent(eventId, options = {}) {
const query = {
text: `
SELECT
*
FROM
balance_operations
WHERE
originator_id = $1
ORDER BY
created_at ASC
;`,
values: [eventId],
};

const results = await database.query(query, options);

return results.rows;
}
}
}

Expand Down
88 changes: 21 additions & 67 deletions models/content-tabcoin.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,5 @@
import database from 'infra/database';

async function findAll(values = {}, options) {
const where = values.where ?? {};
const whereClause = buildWhereClause(where);

const limitClause = values.limit ? `LIMIT ${values.limit}` : '';

const query = {
text: `
SELECT
*
FROM
content_tabcoin_operations
${whereClause}
${limitClause}
;`,
values: Object.values(where),
};

const results = await database.query(query, options);
return results.rows;

function buildWhereClause(where) {
const columnMap = {
balanceType: 'balance_type',
contentId: 'content_id',
originatorType: 'originator_type',
originatorId: 'originator_id',
};

const conditions = Object.entries(where).map(([key, value], _index) => {
const index = _index + 1;
const column = columnMap[key] ?? key;
return Array.isArray(value) ? `${column} = ANY ($${index})` : `${column} = $${index}`;
});

return conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
}
}

async function findOne(operationId, options) {
const rows = await findAll(
{
limit: 1,
where: {
id: operationId,
},
},
options,
);

return rows[0];
}

async function create({ balanceType, contentId, amount, originatorType, originatorId }, options) {
const query = {
text: `
Expand Down Expand Up @@ -83,25 +30,32 @@ async function getTabcoinsCreditDebit({ contentId }, options) {
};
}

async function undo(operationId, options) {
const balanceOperation = await findOne(operationId, options);

const invertedBalanceOperation = {
balanceType: balanceOperation.balance_type,
contentId: balanceOperation.content_id,
amount: balanceOperation.amount * -1,
originatorType: 'event',
originatorId: options.event.id,
async function undoAllByOriginatorId(originatorId, options) {
const query = {
text: `
INSERT INTO content_tabcoin_operations
(balance_type, content_id, amount, originator_type, originator_id)
SELECT
balance_type,
content_id,
amount * -1,
'event',
$1
FROM
content_tabcoin_operations
WHERE
originator_id = $2
RETURNING *;
`,
values: [options.event.id, originatorId],
};

const newBalanceOperation = await create(invertedBalanceOperation, options);
return newBalanceOperation;
const results = await database.query(query, options);
return results.rows;
}

export default Object.freeze({
create,
findAll,
findOne,
getTabcoinsCreditDebit,
undo,
undoAllByOriginatorId,
});
Loading

0 comments on commit be9132c

Please sign in to comment.