Skip to content

Commit

Permalink
feat: add required serialnumberpattern
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 18, 2022
1 parent 82bc2f3 commit a5e5403
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion migrations/20211212200953-fulltext-search.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
customSerialNumberPattern,
serialNumberPattern,
unitIdentifier,
unitType,
intendedBuyerOrgUid,
Expand Down
8 changes: 4 additions & 4 deletions migrations/20211219184405-sqlite-triggers-units.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
customSerialNumberPattern,
serialNumberPattern,
unitIdentifier,
unitType,
intendedBuyerOrgUid,
Expand All @@ -36,7 +36,7 @@ module.exports = {
new.countryJurisdictionOfOwner,
new.inCountryJurisdictionOfOwner,
new.serialNumberBlock,
new.customSerialNumberPattern,
new.serialNumberPattern,
new.unitIdentifier,
new.unitType,
new.intendedBuyerOrgUid,
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = {
countryJurisdictionOfOwner,
inCountryJurisdictionOfOwner,
serialNumberBlock,
customSerialNumberPattern,
serialNumberPattern,
unitIdentifier,
unitType,
intendedBuyerOrgUid,
Expand All @@ -96,7 +96,7 @@ module.exports = {
new.countryJurisdictionOfOwner,
new.inCountryJurisdictionOfOwner,
new.serialNumberBlock,
new.customSerialNumberPattern,
new.serialNumberPattern,
new.unitIdentifier,
new.unitType,
new.intendedBuyerOrgUid,
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"socket.io": "^4.4.0",
"socket.io-client": "^4.4.0",
"sqlite3": "4.1.1",
"updeep": "^1.2.1",
"uuidv4": "^6.2.12"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export const split = async (req, res) => {

const { unitBlockStart } = assertSumOfSplitUnitsIsValid(
originalRecord.serialNumberBlock,
new RegExp(originalRecord.serialNumberPattern),
req.body.records,
);

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/fullnode/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './fullnode';
export * from './dispatcher';
5 changes: 4 additions & 1 deletion src/models/units/units.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ module.exports = {
countryJurisdictionOfOwner: Sequelize.STRING,
inCountryJurisdictionOfOwner: Sequelize.STRING,
serialNumberBlock: Sequelize.STRING,
customSerialNumberPattern: Sequelize.STRING,
serialNumberPattern: {
type: Sequelize.STRING,
defaultValue: '[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$',
},
unitIdentifier: Sequelize.STRING,
unitType: Sequelize.STRING,
intendedBuyerOrgUid: Sequelize.STRING,
Expand Down
5 changes: 5 additions & 0 deletions src/models/units/units.stub.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"unitOwnerOrgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"countryJurisdictionOfOwner": "USA",
"serialNumberBlock": "AXJJFSLGHSHEJ1000-AXJJFSLGHSHEJ1010",
"serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$",
"unitIdentifier": "XYZ",
"unitType": "heard reduction",
"intendedBuyerOrgUid": "70150fde-57f6-44a6-9486-1fef49528475",
Expand All @@ -30,6 +31,7 @@
"unitOwnerOrgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"countryJurisdictionOfOwner": "USA",
"serialNumberBlock": "AXJJFSLGHSHEJ2000-AXJJFSLGHSHEJ2010",
"serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$",
"unitIdentifier": "XYZ",
"unitType": "heard reduction",
"intendedBuyerOrgUid": "70150fde-57f6-44a6-9486-1fef49528475",
Expand All @@ -54,6 +56,7 @@
"unitOwnerOrgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"countryJurisdictionOfOwner": "USA",
"serialNumberBlock": "AXJJFSLGHSHEJ3000-AXJJFSLGHSHEJ3010",
"serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$",
"unitIdentifier": "XYZ",
"unitType": "heard reduction",
"intendedBuyerOrgUid": "70150fde-57f6-44a6-9486-1fef49528475",
Expand All @@ -78,6 +81,7 @@
"unitOwnerOrgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"countryJurisdictionOfOwner": "USA",
"serialNumberBlock": "AXJJFSLGHSHEJ4000-AXJJFSLGHSHEJ4010",
"serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$",
"unitIdentifier": "XYZ",
"unitType": "heard reduction",
"intendedBuyerOrgUid": "70150fde-57f6-44a6-9486-1fef49528475",
Expand All @@ -101,6 +105,7 @@
"orgUid": "35f92331-c8d7-4e9e-a8d2-cd0a86cbb2cf",
"unitOwnerOrgUid": "f1c54511-865e-4611-976c-7c3c1f704662",
"serialNumberBlock": "AXJJFSLGHSHEJ5000-AXJJFSLGHSHEJ5010",
"serialNumberPattern": "[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$",
"unitIdentifier": "XYZ",
"unitType": "heard reduction",
"countryJurisdictionOfOwner": "USA",
Expand Down
7 changes: 5 additions & 2 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,18 @@ export const assertProjectRecordExists = async (

export const assertSumOfSplitUnitsIsValid = (
serialNumberBlock,
serialNumberPattern,
splitRecords,
) => {
const sumOfSplitUnits = splitRecords.reduce(
(previousValue, currentValue) =>
previousValue.unitCount + currentValue.unitCount,
);

const [unitBlockStart, unitBlockEnd, unitCount] =
transformSerialNumberBlock(serialNumberBlock);
const [unitBlockStart, unitBlockEnd, unitCount] = transformSerialNumberBlock(
serialNumberBlock,
serialNumberPattern,
);

if (sumOfSplitUnits !== unitCount) {
throw new Error(
Expand Down
3 changes: 2 additions & 1 deletion src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export const defaultSerialNumberPattern = /[.*\D]+([0-9]+)+[-][.*\D]+([0-9]+)$/;
export const transformSerialNumberBlock = (
serialNumberBlock,
// serial number format: ABC1000-ABC1010
serialNumberPattern = defaultSerialNumberPattern,
serialNumberPattern,
) => {
console.log(serialNumberPattern);
const unitBlocks = serialNumberBlock.match(serialNumberPattern);

if (!unitBlocks) {
Expand Down
9 changes: 6 additions & 3 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { transformSerialNumberBlock } from '../utils/helpers';
import { newVintageScheme, existingVintageSchema } from './vintages.validation';

const customSerialNumberValidator = (obj, helper) => {
const { serialNumberBlock, customSerialNumberPattern } = obj;
const { serialNumberBlock, serialNumberPattern } = obj;

// eslint-disable-next-line no-unused-vars
const [_, __, unitCount] = transformSerialNumberBlock(
serialNumberBlock,
customSerialNumberPattern || undefined,
new RegExp(serialNumberPattern),
);

if (!unitCount) {
Expand All @@ -30,7 +30,10 @@ const unitsBaseSchema = {
inCountryJurisdictionOfOwner: Joi.string().optional(),
// must be in the form ABC123-XYZ456
serialNumberBlock: Joi.string().required(),
customSerialNumberPattern: Joi.string().optional(),
serialNumberPattern: Joi.string().required().messages({
'any.required':
'serialNumberPattern is required. This pattern must be a regex expression with 2 match groups to match block start and block end. Example: [.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$ that matches ABC1000-ABC1010 TODO: ADD LINK HERE FOR DOCUMENTATION',
}),
unitIdentifier: Joi.string().required(),
unitType: Joi.string().valid('heard reduction', 'removal').required(),
intendedBuyerOrgUid: Joi.string().optional(),
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Create Unit Integration', function () {
// create and commit the unit to be deleted
const payload = {
serialNumberBlock: 'AXJJFSLGHSHEJ9000-AXJJFSLGHSHEJ9010',
serialNumberPattern: '[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$',
countryJurisdictionOfOwner: 'USA',
unitType: 'removal',
unitIdentifier: 'XYZ',
Expand Down Expand Up @@ -360,6 +361,7 @@ describe('Create Unit Integration', function () {

const payload = {
serialNumberBlock: 'AXJJFSLGHSHEJ9000-AXJJFSLGHSHEJ9010',
serialNumberPattern: '[.*\\D]+([0-9]+)+[-][.*\\D]+([0-9]+)$',
countryJurisdictionOfOwner: 'USA',
unitType: 'removal',
unitIdentifier: 'XYZ',
Expand Down

0 comments on commit a5e5403

Please sign in to comment.