diff --git a/.gitignore b/.gitignore index 249e90fb..58177d69 100755 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,12 @@ coverage *.iml package-lock.json .DS_Store -build/ \ No newline at end of file +build/ + + + + +.bin +.vs +.obj + diff --git a/Nomination.njsproj b/Nomination.njsproj new file mode 100644 index 00000000..99533180 --- /dev/null +++ b/Nomination.njsproj @@ -0,0 +1,297 @@ + + + + Debug + 2.0 + {00e38c43-3f57-4058-a731-1e207abf63af} + . + ShowAllFiles + + . + . + {3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{349c5851-65df-11da-9384-00065b846f21};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD} + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + Debug|Any CPU + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + True + 0 + / + http://localhost:48022/ + False + True + http://localhost:1337 + False + + + + + + + CurrentPage + True + False + False + False + + + + + + + + + False + False + + + + + \ No newline at end of file diff --git a/bin/Microsoft.NodejsTools.WebRole.dll b/bin/Microsoft.NodejsTools.WebRole.dll new file mode 100644 index 00000000..879bdfc4 Binary files /dev/null and b/bin/Microsoft.NodejsTools.WebRole.dll differ diff --git a/obj/Debug/Nomination.njsproj.CoreCompileInputs.cache b/obj/Debug/Nomination.njsproj.CoreCompileInputs.cache new file mode 100644 index 00000000..38a222e1 --- /dev/null +++ b/obj/Debug/Nomination.njsproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +07e3dfdc747d235400f294e34b8edca0edec755d diff --git a/obj/Debug/Nomination.njsprojAssemblyReference.cache b/obj/Debug/Nomination.njsprojAssemblyReference.cache new file mode 100644 index 00000000..f202504f Binary files /dev/null and b/obj/Debug/Nomination.njsprojAssemblyReference.cache differ diff --git a/obj/Release/Nomination.njsproj.CoreCompileInputs.cache b/obj/Release/Nomination.njsproj.CoreCompileInputs.cache new file mode 100644 index 00000000..38a222e1 --- /dev/null +++ b/obj/Release/Nomination.njsproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +07e3dfdc747d235400f294e34b8edca0edec755d diff --git a/src/manager/approvedElection/ApprovedElectionManager.js b/src/manager/approvedElection/ApprovedElectionManager.js new file mode 100644 index 00000000..204c8320 --- /dev/null +++ b/src/manager/approvedElection/ApprovedElectionManager.js @@ -0,0 +1,35 @@ +import { ApprovedElection } from 'Models'; +import _ from "lodash"; +import moment from "../candidate/candidateManager"; +import {List} from "typed-immutable"; +var joinjs = require('join-js').default; +// join-js usage : https://www.npmjs.com/package/join-js + +const resultMaps = [ + { + mapId: 'approvedElectionMap', + idProperty: 'ID', + properties: ['NAME', 'CREATED_BY', 'CREATED_AT','UPDATED_AT','MODULE_ID','STATUS','APPROVED_BY','APPROVED_AT'] + } +]; + + +const mapToApprovedElectionsDataModel = (activeApprovedElections) => { + + // return activeApprovedElections; + const mappedActiveElections = joinjs.map(activeApprovedElections, resultMaps, 'approvedElectionMap', 'APPROVED_ELECTION_'); + console.log(mappedActiveElections); + + return _.reduce(mappedActiveElections, function(result, approvedElection) { + return result.push({ + + "name": approvedElection.NAME, + + + }); + },List(ApprovedElection)()) +}; + +export default { + mapToApprovedElectionsDataModel, +}; diff --git a/src/manager/index.js b/src/manager/index.js index 2290e640..522deb48 100644 --- a/src/manager/index.js +++ b/src/manager/index.js @@ -10,7 +10,7 @@ import ModuleManager from './module/moduleManager'; import ObjectionManager from './objection/objectionManager'; import ActiveElectionManager from './activeElection/activeElectionManager'; import ElectionNominationManager from './electionNomination/electionNominationManager'; - +import ApprovedElectionManager from './approvedElection/ApprovedElectionManager'; export { UserManager, PaymentManager, @@ -24,4 +24,5 @@ export { ObjectionManager, ActiveElectionManager, ElectionNominationManager, + ApprovedElectionManager, } diff --git a/src/model/approvedElection.js b/src/model/approvedElection.js new file mode 100644 index 00000000..21a8306b --- /dev/null +++ b/src/model/approvedElection.js @@ -0,0 +1,8 @@ + +import {Record} from 'typed-immutable'; + +export const Candidate = Record({ + + name:String(), + +}); \ No newline at end of file diff --git a/src/model/index.js b/src/model/index.js index 5ac93f91..9b40ad77 100644 --- a/src/model/index.js +++ b/src/model/index.js @@ -10,7 +10,8 @@ import { SupportDoc,CandidateSupportDoc } from './SupportDoc'; import {Module} from './Module'; import {Objection } from './Objection'; import {ActiveElection } from './ActiveElection'; -import {ElectionNomination} from './ElectionNomination' +import {ElectionNomination} from './ElectionNomination'; +import {ApprovedElection} from './approvedElection'; export { @@ -30,4 +31,5 @@ export { ActiveElection, ElectionNomination, AllowedDivision, + ApprovedElection, }; diff --git a/src/repository/dataSource.js b/src/repository/dataSource.js index d7e00976..31c94166 100644 --- a/src/repository/dataSource.js +++ b/src/repository/dataSource.js @@ -17,10 +17,12 @@ class DataSourceFactory { throw new Error('Cannot construct singleton'); } + this.dbConnection = new Sequelize(configService.getConfig('DB_NAME'),configService.getConfig('DB_USER'), configService.getConfig('DB_PASSWORD'), configService.getConfig('DB_USER'), { host: configService.getConfig('DB_HOST') }); + } /** diff --git a/src/repository/election.js b/src/repository/election.js index 7ee65068..d3a285c1 100644 --- a/src/repository/election.js +++ b/src/repository/election.js @@ -75,10 +75,37 @@ const fetchElectionsByStatus = (status) => { }); } +const APPROVED_ELECTION_SELECT_QUERY = `SELECT +E.ID AS APPROVED_ELECTION_ID, +E.NAME AS APPROVED_ELECTION_NAME, +E.CREATED_BY AS APPROVED_ELECTION_CREATED_BY, +E.CREATED_AT AS APPROVED_ELECTION_CREATED_AT, +E.UPDATED_AT AS APPROVED_ELECTION_UPDATED_AT, +E.MODULE_ID AS APPROVED_ELECTION_MODULE_ID, +EA.STATUS AS APPROVED_ELECTION_STATUS, +EA.APPROVED_BY AS APPROVED_ELECTION_APPROVED_BY, +EA.APPROVED_AT AS APPROVED_ELECTION_APPROVED_AT, +EA.UPDATED_AT AS APPROVED_ELECTION_UPDATED_AT +FROM ELECTION AS E +INNER JOIN election_approval AS EA +ON E.ID = EA.ELECTION_ID +WHERE EA.STATUS="APPROVE"`; +const fetchApprovedElections=()=>{ + return DbConnection() + .query(APPROVED_ELECTION_SELECT_QUERY, { + //replacements: params, + type: DbConnection().QueryTypes.SELECT, + }).catch( (error) => { + console.log(error); + throw new DBError(error); + }); +} + export default { fetchElectionByIdWithTimelineData, fetchAllElections, fetchElectionsByStatus, + fetchApprovedElections, } diff --git a/src/repository/review.js b/src/repository/review.js new file mode 100644 index 00000000..ea1635ac --- /dev/null +++ b/src/repository/review.js @@ -0,0 +1,48 @@ +import { DBError } from 'Errors'; +import { DbConnection } from './dataSource'; + + +const PAYMENT_STATUS_UPDATE_QUERY = `UPDATE PAYMENT + SET + STATUS = :status + WHERE + ID = :paymentId`; + +const updatePaymentStatus = (paymentId,status) => { + const params = { 'status': status, 'paymentId': paymentId}; + return DbConnection() + .query(PAYMENT_STATUS_UPDATE_QUERY, + { + replacements: params, + type: DbConnection().QueryTypes.UPDATE, + }).then((results) => { + return params; + }).catch((error) => { + throw new DBError(error); + }); +}; +//updatePaymentNote + +const PAYMENT_NOTE_UPDATE_QUERY = `UPDATE PAYMENT + SET + NOTE = :note + WHERE + ID = :paymentId`; + +const updatePaymentNote = (paymentId, note) => { + const params = { 'note': note, 'paymentId': paymentId }; + return DbConnection() + .query(PAYMENT_NOTE_UPDATE_QUERY, + { + replacements: params, + type: DbConnection().QueryTypes.UPDATE, + }).then((results) => { + return params; + }).catch((error) => { + throw new DBError(error); + }); +}; +export default { + updatePaymentStatus, + updatePaymentNote, +} diff --git a/src/routes/activeElectionRouter.js b/src/routes/activeElectionRouter.js index 0520949b..18f849ad 100644 --- a/src/routes/activeElectionRouter.js +++ b/src/routes/activeElectionRouter.js @@ -53,5 +53,15 @@ export const initActiveElectionRouter = (app) => { .catch(error => next(error)); }, }, + { + method: GET, + path: '/activeElectionsData', + handler: (req, res, next) => { + return ActiveElectionService.getApprovedElectionData(req) + .then((result) => res.status(200).send(result)) + .catch(error => next(error)); + }, + }, + ]); }; diff --git a/src/routes/constants/URLSchema.js b/src/routes/constants/URLSchema.js index a392725b..a4269820 100644 --- a/src/routes/constants/URLSchema.js +++ b/src/routes/constants/URLSchema.js @@ -1,6 +1,7 @@ import Joi from 'joi'; export const URL_SCHEMA = Joi.object().keys({ + userId: Joi.string().max(36).regex(/^[A-Za-z0-9-]+$/), teamId: Joi.string().max(36).regex(/^[A-Za-z0-9-]+$/), nominationId: Joi.string().max(36).regex(/^[A-Za-z0-9-]+$/), @@ -14,4 +15,6 @@ export const URL_SCHEMA = Joi.object().keys({ electionNominationId: Joi.string().max(36).regex(/^[A-Za-z0-9-]+$/), status: Joi.string().max(10).regex(/^[A-Za-z-]+$/), category: Joi.string().max(20), + paymentId: Joi.string().max(36).regex(/^[A-Za-z0-9-]+$/), + }); diff --git a/src/routes/paymentRouter.js b/src/routes/paymentRouter.js index 6efc4c95..8f6420e3 100644 --- a/src/routes/paymentRouter.js +++ b/src/routes/paymentRouter.js @@ -6,18 +6,18 @@ import { createRoutes } from '../middleware/Router'; const paymentRouter = createRoutes(); export const initPaymentRouter = (app) => { - paymentRouter(app, [ - { - // curl -H "Content-Type: application/json" -X GET http://localhost:9001/ec-election/elections/43680f3e-97ac-4257-b27a-5f3b452da2e6/payments - method: GET, - path: '/elections/:electionId/payments', - schema: {}, - handler: (req, res, next) => { - return PaymentService.getPaymentsByElectionId(req) - .then((result) => res.status(200).send(result)) - .catch(error => next(error)); + paymentRouter(app, [ + { + // curl -H "Content-Type: application/json" -X GET http://localhost:9001/ec-election/elections/43680f3e-97ac-4257-b27a-5f3b452da2e6/payments + method: GET, + path: '/elections/:electionId/payments', + schema: {}, + handler: (req, res, next) => { + return PaymentService.getPaymentsByElectionId(req) + .then((result) => res.status(200).send(result)) + .catch(error => next(error)); - }, - } - ]); -}; + }, + } + ]); +}; \ No newline at end of file diff --git a/src/routes/reviewRouter.js b/src/routes/reviewRouter.js new file mode 100644 index 00000000..b63055fb --- /dev/null +++ b/src/routes/reviewRouter.js @@ -0,0 +1,35 @@ +import _ from 'lodash'; +import { GET, POST ,PUT} from 'HttpMethods'; +import { reviewService } from 'Service'; +import { createRoutes } from '../middleware/Router'; + +const paymentRouter = createRoutes(); + +export const initReviewRouter = (app) => { + paymentRouter(app, [ + { + // curl -H "Content-Type: application/json" -X GET http://localhost:9001/ec-election/review/378a33e1-5ad0-42f1-9403-dc9dbba32f4c/payments + method: PUT, + path: '/review/:paymentId/payments', + // schema: {}, + handler: (req, res, next) => { + return reviewService.putPaymentsBypaymentId(req) + .then((result) => res.status(200).send(result)) + .catch(error => next(error)); + + }, + }, + { + // curl -H "Content-Type: application/json" -X GET http://localhost:9001/ec-election/payments/378a33e1-5ad0-42f1-9403-dc9dbba32f4c/note + method: PUT,// /payments/:payment_id/note + path: '/payments/:paymentId/note', + // schema: {}, + handler: (req, res, next) => { + return reviewService.putPaymentNoteBypaymentId(req) + .then((result) => res.status(200).send(result)) + .catch(error => next(error)); + + }, + }, + ]); +}; diff --git a/src/routes/routerAggregator.js b/src/routes/routerAggregator.js index 4536d669..6e7b3d94 100644 --- a/src/routes/routerAggregator.js +++ b/src/routes/routerAggregator.js @@ -1,27 +1,29 @@ -import { initUserRouter } from './userRouter'; -import { initDefaultRouter } from './defaultRouter'; -import { initTeamRouter } from './teamRouter'; -import { initNominationRouter } from './nominationRouter'; -import { initElectionRouter } from './electionRouter'; -import { initDivisionRouter } from './divisionRouter'; -import { initModuleRouter } from './moduleRouter'; -import { initObjectionRouter } from './objectionRouter'; -import { initActiveElectionRouter } from './activeElectionRouter'; -import { initElectionNominationRouter } from './electionNominationRouter'; -import { initPaymentRouter } from './paymentRouter'; +import {initUserRouter} from './userRouter'; +import {initDefaultRouter} from './defaultRouter'; +import {initTeamRouter} from './teamRouter'; +import {initNominationRouter} from './nominationRouter'; +import {initElectionRouter} from './electionRouter'; +import {initDivisionRouter} from './divisionRouter'; +import {initModuleRouter} from './moduleRouter'; +import {initObjectionRouter} from './objectionRouter'; +import {initActiveElectionRouter} from './activeElectionRouter'; +import {initElectionNominationRouter} from './electionNominationRouter'; +import {initPaymentRouter} from './paymentRouter'; +import { initReviewRouter } from './reviewRouter'; export const initRoutes = (app) => { - initUserRouter(app); - initDefaultRouter(app); - initTeamRouter(app); - initObjectionRouter(app); - initNominationRouter(app); - initElectionRouter(app); - initDivisionRouter(app); - initModuleRouter(app); - initObjectionRouter(app); - initActiveElectionRouter(app); - initElectionNominationRouter(app); - initPaymentRouter(app); + initUserRouter(app); + initDefaultRouter(app); + initTeamRouter(app); + initObjectionRouter(app); + initNominationRouter(app); + initElectionRouter(app); + initDivisionRouter(app); + initModuleRouter(app); + initObjectionRouter(app); + initActiveElectionRouter(app); + initElectionNominationRouter(app); + initPaymentRouter(app); + initReviewRouter(app); }; diff --git a/src/service/activeElectionService.js b/src/service/activeElectionService.js index 8fb1ec76..cdd8a0f2 100644 --- a/src/service/activeElectionService.js +++ b/src/service/activeElectionService.js @@ -1,8 +1,10 @@ import { ServerError , ApiError } from 'Errors'; import ActiveElectionRepo from '../repository/activeElection'; -import {ActiveElectionManager} from 'Managers'; +import {ActiveElectionManager,ApprovedElectionManager} from 'Managers'; import _ from 'lodash'; import { executeTransaction } from '../repository/TransactionExecutor'; +import ElectionRepo from "../repository/election"; +import {HTTP_CODE_404} from "../routes/constants/HttpCodes"; const uuidv4 = require('uuid/v4'); @@ -87,9 +89,17 @@ const getActiveElectionByActiveElectionId = async (req) => { throw new ApiError("ActiveElection not found"); } }; - +const getApprovedElectionData =async () =>{ + const elections = await ElectionRepo.fetchApprovedElections(); + if(!_.isEmpty(elections)){ + return ApprovedElectionManager.mapToApprovedElectionsDataModel(elections); + } else { + throw new ApiError("No Election found"); + } +} export default { getActiveElectionByActiveElectionId, updateActiveElectionByActiveElectionId, - saveActiveElectionData + saveActiveElectionData, + getApprovedElectionData } \ No newline at end of file diff --git a/src/service/candidateService.js b/src/service/candidateService.js index af01b12c..e7c5c4ce 100644 --- a/src/service/candidateService.js +++ b/src/service/candidateService.js @@ -10,6 +10,7 @@ const uuidv4 = require('uuid/v4'); //Get candidate details for a particular nomination const getCandidateListByNominationId = async (req) => { + try { const nomination_id = req.params.nominationId; const nomination = await NominationService.validateNominationId(nomination_id); @@ -28,10 +29,12 @@ const getCandidateListByNominationId = async (req) => { console.log(e); throw new ServerError("server error"); } + }; //Get candidate for a particular nomination by candidateId and nominationId const getCandidateByNominationId = async (req) => { + try { const nominationId = req.params.nominationId; const candidateId = req.params.candidateId; @@ -45,6 +48,7 @@ const getCandidateByNominationId = async (req) => { } catch (e) { throw new ServerError("server error", HTTP_CODE_404); } + }; @@ -53,16 +57,16 @@ const getCandidateByNominationId = async (req) => { * @param {*} req */ const updateCandidateDataById = async (req) => { - try { - const candidateData = req.body; - const candidateId = req.params.candidateId; - if (isCandidateExists(candidateId)) { - const candidate = { 'id': candidateId, 'fullName': candidateData.fullName, 'preferredName': candidateData.preferredName, 'nic': candidateData.nic, 'dateOfBirth': candidateData.dateOfBirth, 'gender': candidateData.gender, 'address': candidateData.address, 'occupation': candidateData.occupation, 'electoralDivisionName': candidateData.electoralDivisionName, 'electoralDivisionCode': candidateData.electoralDivisionCode, 'counsilName': candidateData.counsilName }; - return await CandidateRepo.updateCandidate(candidate); - } - } catch (error) { - throw new ServerError("server error", HTTP_CODE_404); - } + try { + const candidateData = req.body; + const candidateId = req.params.candidateId; + if (isCandidateExists(candidateId)) { + const candidate = { 'id': candidateId, 'fullName': candidateData.fullName, 'preferredName': candidateData.preferredName, 'nic': candidateData.nic, 'dateOfBirth': candidateData.dateOfBirth, 'gender': candidateData.gender, 'address': candidateData.address, 'occupation': candidateData.occupation, 'electoralDivisionName': candidateData.electoralDivisionName, 'electoralDivisionCode': candidateData.electoralDivisionCode, 'counsilName': candidateData.counsilName }; + return await CandidateRepo.updateCandidate(candidate); + } + } catch (error) { + throw new ServerError("server error", HTTP_CODE_404); + } } /** @@ -71,20 +75,21 @@ const updateCandidateDataById = async (req) => { * @returns boolean */ const isCandidateExists = async (candidateId) => { - try { - const candidate = await CandidateRepo.getCandidateById(candidateId); - if (!_.isEmpty(candidate)) { - return true; - } else { - return false; - } - } catch (error) { - return false; - } + try { + const candidate = await CandidateRepo.getCandidateById(candidateId); + if (!_.isEmpty(candidate)) { + return true; + } else { + return false; + } + } catch (error) { + return false; + } } //Delete candidate from particular nomination + const deleteCandidateByCandidateId = async (req) => { try { const candidateId = req.params.candidateId; @@ -98,8 +103,11 @@ const isCandidateExists = async (candidateId) => { + //Save candidate const saveCandidateByNominationId = async (req) => { + + try { const id = uuidv4(); const fullName = req.body.fullName; @@ -123,38 +131,39 @@ const saveCandidateByNominationId = async (req) => { } catch (e) { throw new ServerError("server error", HTTP_CODE_404); } + }; //Save candidate support docs const saveCandidateSupportDocsByCandidateId = async (req) => { - try { - const id = uuidv4(); - const filePath = req.body.filePath; - const preferredName = req.body.preferredName; - const nic = req.body.nic; - const dateOfBirth = req.body.dateOfBirth; - const gender = req.body.gender; - const address = req.body.address; - const occupation = req.body.occupation; - const electoralDivisionName = req.body.electoralDivisionName; - const electoralDivisionCode = req.body.electoralDivisionCode; - const counsilName = req.body.counsilName; - const nominationId = req.body.nominationId; - const nomination = await NominationService.validateNominationId(nominationId); - if (!_.isEmpty(nomination)) { - const candidateData = { 'id': id, 'electoralDivisionCode': electoralDivisionCode, 'counsilName': counsilName, 'nominationId': nominationId }; - const candidates = await CandidateRepo.createCandidate(candidateData); - if (candidates == 0) { - return true; - } else { - return false; - } - } else { - throw new ApiError("Nomination not found", HTTP_CODE_204); - } - } catch (e) { - throw new ServerError("server error", HTTP_CODE_404); - } + try { + const id = uuidv4(); + const filePath = req.body.filePath; + const preferredName = req.body.preferredName; + const nic = req.body.nic; + const dateOfBirth = req.body.dateOfBirth; + const gender = req.body.gender; + const address = req.body.address; + const occupation = req.body.occupation; + const electoralDivisionName = req.body.electoralDivisionName; + const electoralDivisionCode = req.body.electoralDivisionCode; + const counsilName = req.body.counsilName; + const nominationId = req.body.nominationId; + const nomination = await NominationService.validateNominationId(nominationId); + if (!_.isEmpty(nomination)) { + const candidateData = { 'id': id, 'electoralDivisionCode': electoralDivisionCode, 'counsilName': counsilName, 'nominationId': nominationId }; + const candidates = await CandidateRepo.createCandidate(candidateData); + if (candidates == 0) { + return true; + } else { + return false; + } + } else { + throw new ApiError("Nomination not found", HTTP_CODE_204); + } + } catch (e) { + throw new ServerError("server error", HTTP_CODE_404); + } }; /** @@ -163,33 +172,33 @@ const saveCandidateSupportDocsByCandidateId = async (req) => { * @return boolean */ const saveCandidateConfig = async (req) => { - let isValidModuleId; - try { - isValidModuleId = await ModuleService.validateModuleId(req.params.moduleId); - } catch (error) { - throw new ApiError("Module not found", HTTP_CODE_204); - } - try { - if (isValidModuleId) { - const moduleId = req.params.moduleId; - const configReceivedData = req.body.candidateConfig; - const configs = ["fullName", "preferredName", "nic", "dateOfBirth", "gender", "address", "occupation", "electoralDivisionName", "electoralDivisionCode", "counsilName"]; - - // check if it is an INSERT or UPDATE - const moduleExists = await isModuleExistAtCandidateConfig(moduleId); - if (!moduleExists) { // INSERT - const configData = await generateFullDatasetJsonObject(configs, configReceivedData); - configData.id = uuidv4(); - configData.moduleId = moduleId; - return CandidateRepo.insertCandidateConfigByModuleId(configData); - } - } else { - throw new ApiError("Module not found", HTTP_CODE_204); - } + let isValidModuleId; + try { + isValidModuleId = await ModuleService.validateModuleId(req.params.moduleId); + } catch (error) { + throw new ApiError("Module not found", HTTP_CODE_204); + } + try { + if (isValidModuleId) { + const moduleId = req.params.moduleId; + const configReceivedData = req.body.candidateConfig; + const configs = ["fullName", "preferredName", "nic", "dateOfBirth", "gender", "address", "occupation", "electoralDivisionName", "electoralDivisionCode", "counsilName"]; - } catch (error) { - throw new ServerError("server error", HTTP_CODE_404); - } + // check if it is an INSERT or UPDATE + const moduleExists = await isModuleExistAtCandidateConfig(moduleId); + if (!moduleExists) { // INSERT + const configData = await generateFullDatasetJsonObject(configs, configReceivedData); + configData.id = uuidv4(); + configData.moduleId = moduleId; + return CandidateRepo.insertCandidateConfigByModuleId(configData); + } + } else { + throw new ApiError("Module not found", HTTP_CODE_204); + } + + } catch (error) { + throw new ServerError("server error", HTTP_CODE_404); + } } @@ -199,16 +208,16 @@ const saveCandidateConfig = async (req) => { * @return boolean */ const isModuleExistAtCandidateConfig = async (moduleId) => { - try { - const configs = await CandidateRepo.getCandidateConfigByModuleId(moduleId); - if (!_.isEmpty(configs)) { - return true; - } else { - return false; - } - } catch (error) { - throw new ApiError("Module not found", HTTP_CODE_204); - } + try { + const configs = await CandidateRepo.getCandidateConfigByModuleId(moduleId); + if (!_.isEmpty(configs)) { + return true; + } else { + return false; + } + } catch (error) { + throw new ApiError("Module not found", HTTP_CODE_204); + } } /** @@ -218,48 +227,48 @@ const isModuleExistAtCandidateConfig = async (moduleId) => { * @returns JSON Object */ const generateFullDatasetJsonObject = async (fullset, subset) => { - let jsonString = "{ "; - fullset.forEach((fullsetItem) => { - if (subset.find((subsetItem) => subsetItem == fullsetItem)) { - jsonString += '"' + fullsetItem + '": true, '; - } else { - jsonString += '"' + fullsetItem + '": false, '; - } - }); - jsonString = jsonString.slice(0, -2); - jsonString += " }"; + let jsonString = "{ "; + fullset.forEach((fullsetItem) => { + if (subset.find((subsetItem) => subsetItem == fullsetItem)) { + jsonString += '"' + fullsetItem + '": true, '; + } else { + jsonString += '"' + fullsetItem + '": false, '; + } + }); + jsonString = jsonString.slice(0, -2); + jsonString += " }"; - return JSON.parse(jsonString); + return JSON.parse(jsonString); } const saveCandidateSupportDocConfigData = async (req) => { - let isValidModuleId; - try { - isValidModuleId = await ModuleService.validateModuleId(req.params.moduleId); - } catch (error) { - throw new ApiError("Module not found", HTTP_CODE_204); - } - try { - if (isValidModuleId) { - const moduleId = req.params.moduleId; - const supportDocConfigReceivedData = req.body.supportDocConfigData; - const val = { "params": { "category": "CANDIDATE" } }; - const candidateSupportDocConfig = await SupportDocService.getsupportDocsByCategory(val); - - const supportDocs = supportDocConfigReceivedData.map(data => { - return ({ - "SUPPORT_DOC_CONFIG_ID": candidateSupportDocConfig.find(doc => _.camelCase(doc.keyName) === data).id, // filter data for requested docs - "MODULE_ID": moduleId, - "SELECT_FLAG": true - }); - }); - - return await SupportDocRepo.insertSupportDocConfigData(supportDocs); - } - } catch (error) { - throw new ServerError("server error", HTTP_CODE_404); - } + let isValidModuleId; + try { + isValidModuleId = await ModuleService.validateModuleId(req.params.moduleId); + } catch (error) { + throw new ApiError("Module not found", HTTP_CODE_204); + } + try { + if (isValidModuleId) { + const moduleId = req.params.moduleId; + const supportDocConfigReceivedData = req.body.supportDocConfigData; + const val = { "params": { "category": "CANDIDATE" } }; + const candidateSupportDocConfig = await SupportDocService.getsupportDocsByCategory(val); + + const supportDocs = supportDocConfigReceivedData.map(data => { + return ({ + "SUPPORT_DOC_CONFIG_ID": candidateSupportDocConfig.find(doc => _.camelCase(doc.keyName) === data).id, // filter data for requested docs + "MODULE_ID": moduleId, + "SELECT_FLAG": true + }); + }); + + return await SupportDocRepo.insertSupportDocConfigData(supportDocs); + } + } catch (error) { + throw new ServerError("server error", HTTP_CODE_404); + } } @@ -273,3 +282,4 @@ export default { saveCandidateSupportDocConfigData, deleteCandidateByCandidateId } + diff --git a/src/service/index.js b/src/service/index.js index 68e4fdde..80102c2e 100644 --- a/src/service/index.js +++ b/src/service/index.js @@ -10,18 +10,20 @@ import ModuleService from './moduleService'; import ObjectionService from './objectionService'; import ActiveElectionService from './activeElectionService'; import ElectionNominationService from './electionNominationService'; +import reviewService from './reviewService'; export { - UserService, - PaymentService, - NominationService, - CandidateService, - TeamService, - SupportDocService, - ElectionService, - DivisionService, - ModuleService, - ObjectionService, - ActiveElectionService, - ElectionNominationService, + UserService, + PaymentService, + NominationService, + CandidateService, + TeamService, + SupportDocService, + ElectionService, + DivisionService, + ModuleService, + ObjectionService, + ActiveElectionService, + ElectionNominationService, + reviewService, } diff --git a/src/service/reviewService.js b/src/service/reviewService.js new file mode 100644 index 00000000..b18465ce --- /dev/null +++ b/src/service/reviewService.js @@ -0,0 +1,33 @@ +import _ from 'lodash'; +import { ServerError, ApiError } from 'Errors'; +import Payment from '../repository/payment'; +import ReviewRepo from '../repository/review'; +import { PaymentManager } from 'Managers'; +import { NominationService } from 'Service'; +import { HTTP_CODE_404, HTTP_CODE_204 } from '../routes/constants/HttpCodes'; +const uuidv4 = require('uuid/v4'); + + +const putPaymentsBypaymentId = async (req) => { + try{ + return await ReviewRepo.updatePaymentStatus(req.params.paymentId, req.body.status); + } catch (e) { + throw new ServerError("server error"); + } +}; +const putPaymentNoteBypaymentId = async (req) => { + try { + return await ReviewRepo.updatePaymentNote(req.params.paymentId, req.body.note); + } + catch (e) { + throw new ServerError("server error"); + } +} + + + + +export default { + putPaymentsBypaymentId, + putPaymentNoteBypaymentId +}