Skip to content

Commit

Permalink
feat: add custom validation for the serialnumberblock
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 12, 2022
1 parent 169fcc4 commit 88d47c0
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 221 deletions.
6 changes: 3 additions & 3 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
columnsToInclude,
optionallyPaginatedResponse,
paginationParams,
} from './helpers';
} from '../utils/helpers';

export const create = async (req, res) => {
const newRecord = _.cloneDeep(req.body);
Expand Down Expand Up @@ -57,7 +57,7 @@ export const create = async (req, res) => {
export const findAll = async (req, res) => {
let { page, limit, search, orgUid, columns } = req.query;
let where = orgUid ? { orgUid } : undefined;

const includes = [
ProjectLocation,
Qualification,
Expand Down Expand Up @@ -98,7 +98,7 @@ export const findAll = async (req, res) => {
...columnsToInclude(columns, includes),
...paginationParams(page, limit),
};

results = await Project.findAndCountAll({
distinct: true,
where,
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import _ from 'lodash';

import { uuid as uuidv4 } from 'uuidv4';

import {
Staging,
UnitMock,
Expand All @@ -15,13 +16,14 @@ import {
RelatedProject,
Project,
} from '../models';

import {
columnsToInclude,
optionallyPaginatedResponse,
paginationParams,
transformSerialNumberBlock,
createSerialNumberStr,
} from './helpers';
} from '../utils/helpers';

export const create = async (req, res, next) => {
try {
Expand Down Expand Up @@ -58,7 +60,7 @@ export const create = async (req, res, next) => {
export const findAll = async (req, res) => {
let { page, limit, columns, orgUid } = req.query;
let where = orgUid ? { orgUid } : undefined;

const includes = [Qualification];

if (columns) {
Expand Down
2 changes: 1 addition & 1 deletion src/models/ratings/ratings.model.mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Sequelize from 'sequelize';
const { Model } = Sequelize;

import { sequelizeMirror } from '../database';
import ModelTypes from './rating.modeltypes.cjs';
import ModelTypes from './ratings.modeltypes.cjs';

class RatingMirror extends Model {}

Expand Down
3 changes: 0 additions & 3 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ const virtualColumns = {
const rawValue = this.getDataValue('serialNumberBlock');
return rawValue.split('-')[0];
},
set(value) {
throw new Error('Do not try to set the `unitBlockStart` value!');
},
},
unitBlockEnd: {
type: Sequelize.VIRTUAL,
Expand Down
74 changes: 19 additions & 55 deletions src/routes/v1/resources/projects.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,40 @@
'use strict';

import express from 'express';
import Joi from 'joi';
import { ProjectController } from '../../../controllers';
import joiExpress from 'express-joi-validation';

import {
projectsPostSchema,
projectsGetQuerySchema,
projectsUpdateSchema,
projectsDeleteSchema,
} from '../../../validations';

const validator = joiExpress.createValidator({});
const ProjectRouter = express.Router();

const querySchema = Joi.object()
.keys({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
})
.with('page', 'limit');

ProjectRouter.get('/', validator.query(querySchema), (req, res) => {
ProjectRouter.get('/', validator.query(projectsGetQuerySchema), (req, res) => {
return req.query.warehouseProjectId
? ProjectController.findOne(req, res)
: ProjectController.findAll(req, res);
});

const baseSchema = {
originProjectId: Joi.string().required(),
projectId: Joi.string().required(),
program: Joi.string().required(),
projectName: Joi.string().required(),
projectLink: Joi.string().required(),
projectDeveloper: Joi.string().required(),
sector: Joi.string().required(),
projectType: Joi.string().required(),
coveredByNDC: Joi.number().required(),
NDCLinkage: Joi.string().required(),
projectStatus: Joi.string().required(),
projectStatusDate: Joi.string().required(),
unitMetric: Joi.string().required(),
methodology: Joi.string().required(),
methodologyVersion: Joi.number().required(),
validationApproach: Joi.string().required(),
validationDate: Joi.string().required(),
projectTag: Joi.string().required(),
estimatedAnnualAverageEmissionReduction: Joi.number().required(),
projectLocations: Joi.array().min(1).optional(),
qualifications: Joi.array().min(1).optional(),
vintages: Joi.array().min(1).optional(),
coBenefits: Joi.array().min(1).optional(),
relatedProjects: Joi.array().min(1).optional(),
};

const postSchema = Joi.object({
...baseSchema,
});

ProjectRouter.post('/', validator.body(postSchema), ProjectController.create);

const updateSchema = Joi.object({
warehouseProjectId: Joi.string().required(),
...baseSchema,
});

ProjectRouter.put('/', validator.body(updateSchema), ProjectController.update);
ProjectRouter.post(
'/',
validator.body(projectsPostSchema),
ProjectController.create,
);

const deleteSchema = Joi.object({
warehouseProjectId: Joi.string().required(),
});
ProjectRouter.put(
'/',
validator.body(projectsUpdateSchema),
ProjectController.update,
);

ProjectRouter.delete(
'/',
validator.body(deleteSchema),
validator.body(projectsDeleteSchema),
ProjectController.destroy,
);

Expand Down
9 changes: 3 additions & 6 deletions src/routes/v1/resources/staging.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
'use strict';

import express from 'express';
import Joi from 'joi';
import joiExpress from 'express-joi-validation';
import { StagingController } from '../../../controllers';

import { stagingDeleteSchema } from '../../../validations';

const validator = joiExpress.createValidator({});
const StagingRouter = express.Router();

StagingRouter.get('/', (req, res) => {
return StagingController.findAll(req, res);
});

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

StagingRouter.delete(
'/',
validator.body(querySchemaDelete),
validator.body(stagingDeleteSchema),
StagingController.destroy,
);

Expand Down
93 changes: 21 additions & 72 deletions src/routes/v1/resources/units.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,39 @@
'use strict';

import express from 'express';
import Joi from 'joi';
import { UnitController } from '../../../controllers';
import joiExpress from 'express-joi-validation';

import {
unitsGetQuerySchema,
unitsPostSchema,
unitsUpdateSchema,
unitsDeleteSchema,
unitsSplitSchema,
} from '../../../validations';

const validator = joiExpress.createValidator({});
const UnitRouter = express.Router();

const querySchema = Joi.object()
.keys({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
warehouseUnitId: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
})
.with('page', 'limit');

UnitRouter.get('/', validator.query(querySchema), (req, res) => {
UnitRouter.get('/', validator.query(unitsGetQuerySchema), (req, res) => {
return req.query.warehouseUnitId
? UnitController.findOne(req, res)
: UnitController.findAll(req, res);
});

const baseSchema = {
countryJuridictionOfOwner: Joi.string().required(),
inCountryJuridictionOfOwner: Joi.string().required(),
// must be in the form ABC123-XYZ456
serialNumberBlock: Joi.string()
.regex(/[.*\D]+[0-9]+[-][.*\D]+[0-9]+$/)
.required(),
unitIdentifier: Joi.string().required(),
unitType: Joi.string().valid('heard reduction', 'removal').required(),
intendedBuyerOrgUid: Joi.string().optional(),
marketplace: Joi.string().optional(),
tags: Joi.string().allow('').optional(),
unitStatus: Joi.string().valid('Held', 'For Sale', 'Retired').required(),
unitTransactionType: Joi.string().optional(),
unitStatusReason: Joi.string().optional(),
tokenIssuanceHash: Joi.string().required(),
marketplaceIdentifier: Joi.string().optional(),
unitsIssuanceLocation: Joi.string().optional(),
unitRegistryLink: Joi.string().optional(),
unitMarketplaceLink: Joi.string().optional(),
cooresponingAdjustmentDeclaration: Joi.string()
.valid('Commited', 'Not Required', 'Unknown')
.required(),
correspondingAdjustmentStatus: Joi.string()
.valid('Not Started', 'Pending')
.required(),
};

const postSchema = Joi.object({
...baseSchema,
});

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

const updateSchema = Joi.object({
warehouseUnitId: Joi.string().required(),
...baseSchema,
});

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

const deleteSchema = Joi.object({
warehouseUnitId: Joi.string().required(),
});

UnitRouter.delete('/', validator.body(deleteSchema), UnitController.destroy);
UnitRouter.post('/', validator.body(unitsPostSchema), UnitController.create);
UnitRouter.put('/', validator.body(unitsUpdateSchema), UnitController.update);

const splitSchema = Joi.object({
warehouseUnitId: Joi.string().required(),
records: Joi.array()
.items(
Joi.object().keys({
unitCount: Joi.number().required(),
unitOwnerOrgUid: Joi.string().optional(),
}),
)
.min(2)
.max(2),
});
UnitRouter.delete(
'/',
validator.body(unitsDeleteSchema),
UnitController.destroy,
);

UnitRouter.post('/split', validator.body(splitSchema), UnitController.split);
UnitRouter.post(
'/split',
validator.body(unitsSplitSchema),
UnitController.split,
);

export { UnitRouter };
File renamed without changes.
3 changes: 3 additions & 0 deletions src/validations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './units.validations';
export * from './staging.validations';
export * from './projects.validations';
51 changes: 51 additions & 0 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Joi from 'joi';

export const baseSchema = {
originProjectId: Joi.string().required(),
projectId: Joi.string().required(),
program: Joi.string().required(),
projectName: Joi.string().required(),
projectLink: Joi.string().required(),
projectDeveloper: Joi.string().required(),
sector: Joi.string().required(),
projectType: Joi.string().required(),
coveredByNDC: Joi.number().required(),
NDCLinkage: Joi.string().required(),
projectStatus: Joi.string().required(),
projectStatusDate: Joi.string().required(),
unitMetric: Joi.string().required(),
methodology: Joi.string().required(),
methodologyVersion: Joi.number().required(),
validationApproach: Joi.string().required(),
validationDate: Joi.string().required(),
projectTag: Joi.string().required(),
estimatedAnnualAverageEmissionReduction: Joi.number().required(),
projectLocations: Joi.array().min(1).optional(),
qualifications: Joi.array().min(1).optional(),
vintages: Joi.array().min(1).optional(),
coBenefits: Joi.array().min(1).optional(),
relatedProjects: Joi.array().min(1).optional(),
};

export const projectsGetQuerySchema = Joi.object()
.keys({
page: Joi.number(),
limit: Joi.number(),
search: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
})
.with('page', 'limit');

export const projectsPostSchema = Joi.object({
...baseSchema,
});

export const projectsUpdateSchema = Joi.object({
warehouseProjectId: Joi.string().required(),
...baseSchema,
});

export const projectsDeleteSchema = Joi.object({
warehouseProjectId: Joi.string().required(),
});
5 changes: 5 additions & 0 deletions src/validations/staging.validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Joi from 'joi';

export const stagingDeleteSchema = Joi.object({
uuid: Joi.string().required(),
});
Loading

0 comments on commit 88d47c0

Please sign in to comment.