Skip to content

Commit

Permalink
feat: add split api
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Jan 6, 2022
1 parent 2e1f844 commit bc37fcd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 46 deletions.
2 changes: 1 addition & 1 deletion migrations/20211219184405-sqlite-triggers-units.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {

await queryInterface.sequelize.query(`
CREATE TRIGGER unit_delete_fts AFTER DELETE ON units BEGIN
DELETE FROM unit_insert_fts WHERE id = old.id;
DELETE FROM units_fts WHERE id = old.id;
END;
`);

Expand Down
4 changes: 3 additions & 1 deletion src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ export const commit = async (req, res) => {
uuid,
table,
action,
commited,
data: rawData,
} = stagingRecord;
const data = JSON.parse(rawData);

// set the commited flag to true
await Staging.update(
{ commited: true },
{ where: { id: stagingRecordId } },
Expand All @@ -87,7 +89,7 @@ export const commit = async (req, res) => {
fullNode.deleteProjectRecord(uuid, stagingRecordId);
break;
}
} else if (table === 'Unit' && !commited) {
} else if (table === 'Units' && !commited) {
switch (action) {
case 'INSERT':
fullNode.createUnitRecord(uuid, data, stagingRecordId);
Expand Down
28 changes: 20 additions & 8 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import _ from 'lodash';
import { uuid as uuidv4 } from 'uuidv4';
import { Staging, UnitMock, Unit, Qualification, Vintage } from '../models';
import { optionallyPaginatedResponse, paginationParams } from './helpers';
Expand Down Expand Up @@ -145,27 +146,38 @@ export const destroy = async (req, res) => {
export const split = async (req, res) => {
try {
const originalRecord = await Unit.findOne({
where: { uuid: req.query.unitUid },
where: { uuid: req.body.unitUid },
});

const splitRecords = req.body.records.map((record) => {
const newRecord = _.cloneDeep(originalRecord);
newRecord.uuid = uuidv4();
newRecord.unitCount = record.unitCount;

if (record.orgUid) {
newRecord.orgUid = record.orgUid;
}

return newRecord;
});

const stagedData = {
uuid: req.body.uuid,
uuid: req.body.unitUid,
action: 'UPDATE',
commited: false,
table: 'Units',
data: [
{
...originalRecord,
},
],
data: JSON.stringify(splitRecords),
};

await Staging.create(stagedData);
await Staging.upsert(stagedData);

res.json({
message: 'Unit split successfully',
});
} catch (err) {
res.json({
message: 'Error splitting unit',
err,
});
}
};
16 changes: 14 additions & 2 deletions src/fullnode/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ export const deleteProjectRecord = async (uuid, stagingRecordId) => {
};

export const updateUnitRecord = async (uuid, record, stagingRecordId) => {
const encoded = btoa(JSON.stringify(record));
await simulator.updateUnitRecord(uuid, encoded, stagingRecordId);
if (Array.isArray(record)) {
record.forEach(async (_record, index) => {
const encoded = btoa(JSON.stringify(_record));
await simulator.updateUnitRecord(
// we need to pass in the uuid only one to track
index === record.length - 1 ? uuid : _record.uuid,
encoded,
stagingRecordId,
);
});
} else {
const encoded = btoa(JSON.stringify(record));
await simulator.updateUnitRecord(uuid, record, stagingRecordId);
}
};

export const createUnitRecord = async (uuid, record, stagingRecordId) => {
Expand Down
13 changes: 7 additions & 6 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Project, Unit, Staging } from '../models';
const THIRTY_SEC = 30000;
const THIRTY_SEC = 300;

// Simulate 30 seconds passing before commited to node

Expand All @@ -8,7 +8,7 @@ export const updateProjectRecord = async (
encodedRecord,
stagingRecordId,
) => {
const record = JSON.parse(encodedRecord.toString('utf8'));
const record = JSON.parse(atob(encodedRecord));
return new Promise((resolve) => {
setTimeout(async () => {
if (stagingRecordId) {
Expand Down Expand Up @@ -37,6 +37,7 @@ export const createProjectRecord = (uuid, encodedRecord, stagingRecordId) => {
console.log(record);
return new Promise((resolve) => {
setTimeout(async () => {
delete record.id;
await Project.create({
...record,
warehouseProjectId: uuid,
Expand Down Expand Up @@ -82,10 +83,8 @@ export const updateUnitRecord = async (
encodedRecord,
stagingRecordId,
) => {
const record = JSON.parse(encodedRecord.toString('utf8'));

await deleteUnitRecord(uuid);
await createUnitRecord(uuid, record);
await createUnitRecord(uuid, encodedRecord);

if (stagingRecordId) {
await Staging.destroy({
Expand All @@ -97,9 +96,11 @@ export const updateUnitRecord = async (
};

export const createUnitRecord = (uuid, encodedRecord, stagingRecordId) => {
const record = JSON.parse(encodedRecord.toString('utf8'));
const record = JSON.parse(atob(encodedRecord));

return new Promise((resolve) => {
setTimeout(async () => {
delete record.id;
await Unit.create({
uuid,
...record,
Expand Down
48 changes: 27 additions & 21 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,58 +76,64 @@ class Project extends Model {
if (orgUid) {
sql = `${sql} AND orgUid = :orgUid`;
}

const replacements = { search: searchStr, orgUid };

const count = (await sequelize.query(sql, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements
})).length;

const count = (
await sequelize.query(sql, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements,
})
).length;

if (limit && offset) {
sql = `${sql} ORDER BY relevance DESC LIMIT :limit OFFSET :offset`;
}

return {
count,
rows: await sequelize.query(sql, {
model: Project,
replacements: {...replacements, ...{offset, limit}},
replacements: { ...replacements, ...{ offset, limit } },
mapToModel: true, // pass true here if you have any mapped fields
offset,
limit,
}),
};
}

static async findAllSqliteFts(searchStr, orgUid, pagination) {
const { offset, limit } = pagination;

searchStr = searchStr = searchStr.replaceAll('-', '+');

let sql = `SELECT * FROM projects_fts WHERE projects_fts MATCH :search`;

if (orgUid) {
sql = `${sql} AND orgUid = :orgUid`;
}

const replacements = { search: `${searchStr}*`, orgUid };

const count = (await sequelize.query(sql, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements
})).length;


const count = (
await sequelize.query(sql, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements,
})
).length;

if (limit && offset) {
sql = `${sql} ORDER BY rank DESC LIMIT :limit OFFSET :offset`;
}

return {
count,
rows: await sequelize.query(sql, {
model: Project,
mapToModel: true, // pass true here if you have any mapped fields
replacements: {...replacements, ...{offset, limit}}
replacements: { ...replacements, ...{ offset, limit } },
}),
};
}
Expand Down
29 changes: 22 additions & 7 deletions src/routes/v1/resources/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ UnitRouter.get('/', validator.query(querySchema), (req, res) => {
: UnitController.findAll(req, res);
});

const querySchema = Joi.object({
const bodySchema = Joi.object({
buyer: Joi.string().required(),
registry: Joi.string().required(),
blockIdentifier: Joi.string().required(),
Expand All @@ -40,23 +40,38 @@ const querySchema = Joi.object({
vintageId: Joi.number().required(),
});

UnitRouter.post('/', validator.body(querySchema), UnitController.create);
UnitRouter.post('/', validator.body(bodySchema), UnitController.create);

const querySchemaUpdate = {
const bodySchemaUpdate = {
uuid: Joi.string().required(),
...querySchema,
...bodySchema,
};

UnitRouter.put('/', validator.body(querySchemaUpdate), UnitController.update);
UnitRouter.put('/', validator.body(bodySchemaUpdate), UnitController.update);

const querySchemaDelete = {
const bodySchemaDelete = {
uuid: Joi.string().required(),
};

UnitRouter.delete(
'/',
validator.body(querySchemaDelete),
validator.body(bodySchemaDelete),
UnitController.destroy,
);

const splitSchema = Joi.object({
unitUid: Joi.string().required(),
records: Joi.array()
.items(
Joi.object().keys({
unitCount: Joi.number().required(),
orgUid: Joi.string().optional(),
}),
)
.min(2)
.max(2),
});

UnitRouter.post('/split', UnitController.split);

export { UnitRouter };

0 comments on commit bc37fcd

Please sign in to comment.