Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from digicatapult/chore/projects-enhancement
Browse files Browse the repository at this point in the history
Chore/projects enhancement
  • Loading branch information
darrylmorton-digicatapult authored May 3, 2022
2 parents 060e92d + d90af2f commit f988872
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 212 deletions.
38 changes: 19 additions & 19 deletions app/api-v1/api-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,88 +82,88 @@ const apiDoc = {
required: ['firstName', 'lastName', 'company', 'role'],
},
GetProject: {
description: 'GET project',
description: 'Schema for retrieving a project',
type: 'object',
properties: {
id: {
description: 'Project id',
description: 'Id of the project',
type: 'string',
format: 'uuid',
},
clientId: {
description: 'Project client id',
description: 'Client id of the project',
type: 'string',
format: 'uuid',
},
name: {
description: 'Project Name',
description: 'Name of the project',
type: 'string',
},
description: {
description: 'Project Description',
description: 'Description of the project',
type: 'string',
},
startDate: {
description: 'Project start date',
description: 'Start date of the project',
type: 'object',
format: 'startDate',
format: 'js-date',
nullable: true,
},
endDate: {
description: 'Project end date',
description: 'End date of the project',
type: 'object',
format: 'endDate',
format: 'js-date',
nullable: true,
},
budget: {
description: 'Project budget',
description: 'Budget of the project',
type: 'number',
nullable: true,
},
documentUrl: {
description: 'Project document url',
description: 'Document url of the project',
type: 'string',
nullable: true,
},
},
required: ['id', 'clientId', 'name', 'description'],
},
PostAndPutProject: {
description: 'POST/PUT project',
description: 'Schema for creating/updating a project',
type: 'object',
properties: {
clientId: {
description: 'Project client id',
description: 'Client id of the project',
type: 'string',
format: 'uuid',
},
name: {
description: 'Project Name',
description: 'Name of the project',
type: 'string',
},
description: {
description: 'Project Description',
description: 'Description of the project',
type: 'string',
},
startDate: {
description: 'Project start date',
description: 'Start date of the project',
type: 'string',
format: 'date-time',
nullable: true,
},
endDate: {
description: 'Project end date',
description: 'End date of the project',
type: 'string',
format: 'date-time',
nullable: true,
},
budget: {
description: 'Project budget',
description: 'Budget of the project',
type: 'number',
nullable: true,
},
documentUrl: {
description: 'Project document url',
description: 'Document url of the project',
type: 'string',
nullable: true,
},
Expand Down
25 changes: 19 additions & 6 deletions app/api-v1/routes/profiler/project.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
const { GET_PROJECTS_RESPONSES } = require('../../validators/getProjectsResponseValidator')
const { POST_PROJECT_RESPONSES, validatePostProjectResponse } = require('../../validators/postProjectResponseValidator')
const {
GET_PROJECTS_RESPONSES,
validateGetProjectsResponse,
} = require('../../validators/project/getProjectsResponseValidator')
const {
POST_PROJECT_RESPONSES,
validatePostProjectResponse,
} = require('../../validators/project/postProjectResponseValidator')

module.exports = function (apiService) {
const doc = {
GET: async function (req, res) {
const { statusCode, result } = await apiService.getProjects()

res.status(statusCode).json(result)
return
const validationErrors = validateGetProjectsResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).json(result)
return
}
},
POST: async function (req, res) {
const { statusCode, result } = await apiService.postProject(req.body)
Expand All @@ -25,13 +38,13 @@ module.exports = function (apiService) {
}

doc.GET.apiDoc = {
summary: 'GET projects',
summary: 'Retrieve projects',
responses: GET_PROJECTS_RESPONSES,
tags: ['projects'],
}

doc.POST.apiDoc = {
summary: 'POST project',
summary: 'Create project',
requestBody: {
content: {
'application/json': {
Expand Down
38 changes: 25 additions & 13 deletions app/api-v1/routes/profiler/project/{id}.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const {
validateGetProjectByIdResponse,
GET_PROJECT_BY_ID_RESPONSES,
} = require('../../../validators/getProjectByIdResponseValidator')
const { PUT_PROJECT_RESPONSES, validatePutProjectResponse } = require('../../../validators/putProjectResponseValidator')
} = require('../../../validators/project/getProjectByIdResponseValidator')
const {
PUT_PROJECT_RESPONSES,
validatePutProjectResponse,
} = require('../../../validators/project/putProjectResponseValidator')
const {
DELETE_PROJECT_RESPONSES,
validateDeleteProjectResponse,
} = require('../../../validators/deleteProjectResponseValidator')
} = require('../../../validators/project/deleteProjectResponseValidator')

module.exports = function (apiService) {
const doc = {
Expand Down Expand Up @@ -40,29 +43,32 @@ module.exports = function (apiService) {
},
DELETE: async function (req, res) {
const { id } = req.params
const { statusCode, result } = await apiService.deleteProjectById(id)
const { statusCode, result } = await apiService.deleteProject(id)

const validationErrors = validateDeleteProjectResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).json(result)
res.status(statusCode).send()
return
}
},
}

doc.GET.apiDoc = {
summary: 'Get project by id',
summary: 'Retrieve project by id',
parameters: [
{
description: 'Project id',
description: 'Id of the project',
in: 'path',
required: true,
name: 'id',
allowEmptyValue: false,
schema: {
type: 'string',
format: 'uuid',
},
},
],
responses: GET_PROJECT_BY_ID_RESPONSES,
Expand All @@ -73,11 +79,14 @@ module.exports = function (apiService) {
summary: 'Update project',
parameters: [
{
description: 'ID of the project',
description: 'Id of the project',
in: 'path',
required: true,
name: 'id',
allowEmptyValue: false,
schema: {
type: 'string',
format: 'uuid',
},
},
],
requestBody: {
Expand All @@ -94,14 +103,17 @@ module.exports = function (apiService) {
}

doc.DELETE.apiDoc = {
summary: 'Delete project',
summary: 'Remove project',
parameters: [
{
description: 'Project id',
description: 'Id of the project',
in: 'path',
required: true,
name: 'id',
allowEmptyValue: false,
schema: {
type: 'string',
format: 'uuid',
},
},
],
responses: DELETE_PROJECT_RESPONSES,
Expand Down
40 changes: 20 additions & 20 deletions app/api-v1/services/apiService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {
getProjectsDb,
getProjectByNameDb,
postProjectDb,
getProjectByIdDb,
deleteProjectByIdDb,
findProjectsDb,
findProjectByNameDb,
addProjectDb,
findProjectByIdDb,
removeProjectDb,
updateProjectDb,
addClientDb,
findClientsDb,
Expand Down Expand Up @@ -59,39 +59,39 @@ async function deleteClient(id) {
}

async function getProjects() {
const result = await getProjectsDb()
const result = await findProjectsDb()

return { statusCode: 200, result }
}

async function getProjectById(id) {
const projectsByIdResult = await getProjectByIdDb(id)
const findResult = await findProjectByIdDb(id)

if (projectsByIdResult.length === 1) {
return { statusCode: 200, result: projectsByIdResult[0] }
if (findResult.length === 1) {
return { statusCode: 200, result: findResult[0] }
} else {
return { statusCode: 404, result: {} }
}
}

async function postProject(reqBody) {
const projectByNameResult = await getProjectByNameDb(reqBody.name)
const findResult = await findProjectByNameDb(reqBody.name)

if (projectByNameResult.length === 0) {
const createdProject = await postProjectDb(reqBody)
if (findResult.length === 0) {
const result = await addProjectDb(reqBody)

return { statusCode: 201, result: createdProject[0] }
return { statusCode: 201, result: result[0] }
} else {
return { statusCode: 409, result: {} }
}
}

async function putProject(id, reqBody) {
const projectByIdResult = await getProjectByIdDb(id)
const findResult = await findProjectByIdDb(id)

if (projectByIdResult.length === 0) {
if (findResult.length === 0) {
return { statusCode: 404, result: {} }
} else if (projectByIdResult[0].name !== reqBody.name) {
} else if (findResult[0].name !== reqBody.name) {
const result = await updateProjectDb(id, reqBody)

return { statusCode: 200, result: result[0] }
Expand All @@ -100,11 +100,11 @@ async function putProject(id, reqBody) {
}
}

async function deleteProjectById(id) {
const result = await getProjectByIdDb(id)
async function deleteProject(id) {
const result = await findProjectByIdDb(id)

if (result.length === 1) {
await deleteProjectByIdDb(id)
await removeProjectDb(id)

return { statusCode: 204, result: {} }
} else {
Expand All @@ -121,6 +121,6 @@ module.exports = {
getProjects,
postProject,
getProjectById,
deleteProjectById,
deleteProject,
putProject,
}
21 changes: 0 additions & 21 deletions app/api-v1/validators/getProjectsResponseValidator.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
const OpenAPIResponseValidator = require('openapi-response-validator').default
const { default: OpenAPIResponseValidator } = require('openapi-response-validator')

const apiDocResponses = require('../api-doc-responses')
const apiDoc = require('../api-doc')
const apiDocResponses = require('../../api-doc-responses')

const DELETE_PROJECT_RESPONSES = {
201: {
description: '',
content: {
'application/json': {
schema: apiDoc.components.schemas.GetProject,
},
},
204: {
description: 'Deleted project',
},
404: apiDocResponses['404'],
default: apiDocResponses.default,
Expand Down
Loading

0 comments on commit f988872

Please sign in to comment.