Skip to content

Commit

Permalink
Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Jun 17, 2020
1 parent 1c3ab5e commit b7bb403
Show file tree
Hide file tree
Showing 29 changed files with 577 additions and 358 deletions.
32 changes: 15 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ export class App {
* @return Promise<void>
*/
private setupSwaggerUI(): void {
// Middleware swagger. It should not run in the test environment.
if ((process.env.NODE_ENV || Default.NODE_ENV) !== 'test') {
const options = {
swaggerUrl: Default.SWAGGER_URI,
customCss: '.swagger-ui .topbar { display: none }',
customfavIcon: Default.LOGO_URI,
customSiteTitle: `API Reference | ${Strings.APP.TITLE}`
}
this.express.use('/v1/reference', swaggerUi.serve, swaggerUi.setup({}, options))
const options = {
swaggerUrl: Default.SWAGGER_URI,
customCss: '.swagger-ui .topbar { display: none }',
customfavIcon: Default.LOGO_URI,
customSiteTitle: `API Reference | ${Strings.APP.TITLE}`
}
this.express.use('/v1/reference', swaggerUi.serve, swaggerUi.setup({}, options))
}

/**
Expand All @@ -135,10 +132,12 @@ export class App {
*/
private setupErrorsHandler(): void {
// Handle 404
this.express.use((req: Request, res: Response) => {
const errorMessage: ApiException = new ApiException(404, `${req.url} not found.`,
`Specified resource: ${req.url} was not found or does not exist.`)
res.status(HttpStatus.NOT_FOUND).send(errorMessage.toJson())
this.express.use((req, res) => {
const errorMessage: ApiException = new ApiException(
404,
Strings.ERROR_MESSAGE.ENDPOINT_NOT_FOUND.replace('{0}', req.url)
)
res.status(HttpStatus.NOT_FOUND).send(errorMessage.toJSON())
})

// Handle 400, 500
Expand All @@ -148,11 +147,10 @@ export class App {
if (err && err.statusCode === HttpStatus.BAD_REQUEST) {
statusCode = HttpStatus.BAD_REQUEST
errorMessage.code = statusCode
errorMessage.message = 'Unable to process request body.'
errorMessage.description = 'Please verify that the JSON provided in'
.concat(' the request body has a valid format and try again.')
errorMessage.message = Strings.ERROR_MESSAGE.REQUEST_BODY_INVALID
errorMessage.description = Strings.ERROR_MESSAGE.REQUEST_BODY_INVALID_DESC
}
res.status(statusCode).send(errorMessage.toJson())
res.status(statusCode).send(errorMessage.toJSON())
})
}
}
2 changes: 1 addition & 1 deletion src/application/port/educator.service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IEducatorService extends IService<Educator> {
* @return {Promise<ChildrenGroup>}
* @throws {ValidationException | RepositoryException}
*/
updateChildrenGroup(educatorId: string, childrenGroup: ChildrenGroup): Promise<ChildrenGroup>
updateChildrenGroup(educatorId: string, childrenGroup: ChildrenGroup): Promise<ChildrenGroup | undefined>

/**
* Removes the group of children associated with the educator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IHealthProfessionalService extends IService<HealthProfessional>
* @return {Promise<ChildrenGroup>}
* @throws {ValidationException | RepositoryException}
*/
updateChildrenGroup(healthProfessionalId: string, childrenGroup: ChildrenGroup): Promise<ChildrenGroup>
updateChildrenGroup(healthProfessionalId: string, childrenGroup: ChildrenGroup): Promise<ChildrenGroup | undefined>

/**
* Removes the group of children associated with the health professional.
Expand Down
2 changes: 1 addition & 1 deletion src/application/port/service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface IService<T> {
* @return {Promise<T>}
* @throws {ValidationException | ConflictException | RepositoryException}
*/
update(item: T): Promise<T>
update(item: T): Promise<T | undefined>

/**
* Removes the item according to their unique identifier.
Expand Down
63 changes: 33 additions & 30 deletions src/application/service/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,27 @@ export class ApplicationService implements IApplicationService {
return this._applicationRepository.findOne(query)
}

public async update(application: Application): Promise<Application> {
public async update(application: Application): Promise<Application | undefined> {
try {
// 1. Validate Application parameters.
UpdateApplicationValidator.validate(application)

// 1.5 Ignore last_login attributes if exists.
if (application.last_login) application.last_login = undefined

// 2. Checks if Application already exists.
const id: string = application.id!
application.id = undefined

const applicationExist = await this._applicationRepository.checkExist(application)
if (applicationExist) throw new ConflictException(Strings.APPLICATION.ALREADY_REGISTERED)
// 2. checks if the application exists by id
if (!(await this._applicationRepository.checkExist(application))) {
return Promise.resolve(undefined)
}

application.id = id
// 3. Check if there is already an application with the same username to be updated.
if (application.username) {
const id: string = application.id!
application.id = undefined
if (await this._applicationRepository.checkExist(application)) {
throw new ConflictException(Strings.APPLICATION.ALREADY_REGISTERED)
}
application.id = id
}

// 3. Checks if the institution exists.
// 4. Checks if the institution exists.
if (application.institution && application.institution.id !== undefined) {
const institutionExist = await this._institutionRepository.checkExist(application.institution)
if (!institutionExist) {
Expand All @@ -100,27 +103,27 @@ export class ApplicationService implements IApplicationService {
)
}
}

// 5. Update Application data.
const applicationUp = await this._applicationRepository.update(application)

// 6. If updated successfully, the object is published on the message bus.
if (applicationUp) {
this._eventBus.bus
.pubUpdateApplication(applicationUp)
.then(() => {
this._logger.info(`User of type Application with ID: ${applicationUp.id} has been updated`
.concat(' and published on event bus...'))
})
.catch((err) => {
this._logger.error(`Error trying to publish event UpdateApplication. ${err.message}`)
})
}
// 7. Returns the created object.
return Promise.resolve(applicationUp)
} catch (err) {
return Promise.reject(err)
}

// 4. Update Application data.
const applicationUp = await this._applicationRepository.update(application)

// 5. If updated successfully, the object is published on the message bus.
if (applicationUp) {
this._eventBus.bus
.pubUpdateApplication(applicationUp)
.then(() => {
this._logger.info(`User of type Application with ID: ${applicationUp.id} has been updated`
.concat(' and published on event bus...'))
})
.catch((err) => {
this._logger.error(`Error trying to publish event UpdateApplication. ${err.message}`)
})
}
// 6. Returns the created object.
return Promise.resolve(applicationUp)
}

public async remove(id: string): Promise<boolean> {
Expand Down
64 changes: 33 additions & 31 deletions src/application/service/child.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,27 @@ export class ChildService implements IChildService {
return this._childRepository.findOne(query)
}

public async update(child: Child): Promise<Child> {
public async update(child: Child): Promise<Child | undefined> {
try {
// 1. Validate Child parameters.
UpdateChildValidator.validate(child)

// 1.5 Ignore last_login and last_sync attributes if exists.
if (child.last_login) child.last_login = undefined
if (child.last_sync) child.last_sync = undefined

// 2. Checks if child already exists.
const id: string = child.id!
child.id = undefined

const childExist = await this._childRepository.checkExist(child)
if (childExist) throw new ConflictException(Strings.CHILD.ALREADY_REGISTERED)
// 2. checks if the child exists by id
if (!(await this._childRepository.checkExist(child))) {
return Promise.resolve(undefined)
}

child.id = id
// 3. Check if there is already an child with the same username to be updated.
if (child.username) {
const id: string = child.id!
child.id = undefined
if (await this._childRepository.checkExist(child)) {
throw new ConflictException(Strings.CHILD.ALREADY_REGISTERED)
}
child.id = id
}

// 3. Checks if the institution exists.
// 4. Checks if the institution exists.
if (child.institution && child.institution.id !== undefined) {
const institutionExist = await this._institutionRepository.checkExist(child.institution)
if (!institutionExist) {
Expand All @@ -120,27 +122,27 @@ export class ChildService implements IChildService {
)
}
}

// 5. Update child data.
const childUp = await this._childRepository.update(child)

// 6. If updated successfully, the object is published on the message bus.
if (childUp) {
this._eventBus.bus
.pubUpdateChild(childUp)
.then(() => {
this._logger.info(`User of type Child with ID: ${childUp.id} has been updated`
.concat(' and published on event bus...'))
})
.catch((err) => {
this._logger.error(`Error trying to publish event UpdateChild. ${err.message}`)
})
}
// 7. Returns the created object.
return Promise.resolve(childUp)
} catch (err) {
return Promise.reject(err)
}

// 4. Update child data.
const childUp = await this._childRepository.update(child)

// 5. If updated successfully, the object is published on the message bus.
if (childUp) {
this._eventBus.bus
.pubUpdateChild(childUp)
.then(() => {
this._logger.info(`User of type Child with ID: ${childUp.id} has been updated`
.concat(' and published on event bus...'))
})
.catch((err) => {
this._logger.error(`Error trying to publish event UpdateChild. ${err.message}`)
})
}
// 6. Returns the created object.
return Promise.resolve(childUp)
}

public async remove(id: string): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion src/application/service/children.group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class ChildrenGroupService implements IChildrenGroupService {
return this._childrenGroupRepository.findOne(query)
}

public async update(childrenGroup: ChildrenGroup): Promise<ChildrenGroup> {
public async update(childrenGroup: ChildrenGroup): Promise<ChildrenGroup | undefined> {
try {
// 1. Validate Children Group parameters
UpdateChildrenGroupValidator.validate(childrenGroup)
Expand Down
67 changes: 35 additions & 32 deletions src/application/service/educator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,27 @@ export class EducatorService implements IEducatorService {
return this._educatorRepository.findOne(query)
}

public async update(educator: Educator): Promise<Educator> {
public async update(educator: Educator): Promise<Educator | undefined> {
try {
// 1. Validate Educator parameters.
UpdateEducatorValidator.validate(educator)

// 1.5 Ignore last_login attributes if exists.
if (educator.last_login) educator.last_login = undefined

// 2. Checks if Educator already exists.
const id: string = educator.id!
educator.id = undefined

const educatorExist = await this._educatorRepository.checkExist(educator)
if (educatorExist) throw new ConflictException(Strings.EDUCATOR.ALREADY_REGISTERED)
// 2. checks if the educator exists by id
if (!(await this._educatorRepository.checkExist(educator))) {
return Promise.resolve(undefined)
}

educator.id = id
// 3. Check if there is already an educator with the same username to be updated.
if (educator.username) {
const id: string = educator.id!
educator.id = undefined
if (await this._educatorRepository.checkExist(educator)) {
throw new ConflictException(Strings.EDUCATOR.ALREADY_REGISTERED)
}
educator.id = id
}

// 3. Checks if the institution exists.
// 4. Checks if the institution exists.
if (educator.institution && educator.institution.id !== undefined) {
const institutionExist = await this._institutionRepository.checkExist(educator.institution)
if (!institutionExist) {
Expand All @@ -104,27 +107,27 @@ export class EducatorService implements IEducatorService {
)
}
}

// 5. Update Educator data.
const educatorUp = await this._educatorRepository.update(educator)

// 6. If updated successfully, the object is published on the message bus.
if (educatorUp) {
this._eventBus.bus
.pubUpdateEducator(educatorUp)
.then(() => {
this._logger.info(`User of type Educator with ID: ${educatorUp.id} has been updated`
.concat(' and published on event bus...'))
})
.catch((err) => {
this._logger.error(`Error trying to publish event UpdateEducator. ${err.message}`)
})
}
// 7. Returns the created object.
return Promise.resolve(educatorUp)
} catch (err) {
return Promise.reject(err)
}

// 4. Update Educator data.
const educatorUp = await this._educatorRepository.update(educator)

// 5. If updated successfully, the object is published on the message bus.
if (educatorUp) {
this._eventBus.bus
.pubUpdateEducator(educatorUp)
.then(() => {
this._logger.info(`User of type Educator with ID: ${educatorUp.id} has been updated`
.concat(' and published on event bus...'))
})
.catch((err) => {
this._logger.error(`Error trying to publish event UpdateEducator. ${err.message}`)
})
}
// 6. Returns the created object.
return Promise.resolve(educatorUp)
}

public async remove(id: string): Promise<boolean> {
Expand Down Expand Up @@ -228,7 +231,7 @@ export class EducatorService implements IEducatorService {
return this._childrenGroupService.getById(childrenGroupId, query)
}

public async updateChildrenGroup(educatorId: string, childrenGroup: ChildrenGroup): Promise<ChildrenGroup> {
public async updateChildrenGroup(educatorId: string, childrenGroup: ChildrenGroup): Promise<ChildrenGroup | undefined> {
try {
// 1. Validate if educator id or children group id is valid
ObjectIdValidator.validate(educatorId, Strings.EDUCATOR.PARAM_ID_NOT_VALID_FORMAT)
Expand All @@ -244,7 +247,7 @@ export class EducatorService implements IEducatorService {
}

// 3. Update children group.
const childrenGroupResult: ChildrenGroup = await this._childrenGroupService.update(childrenGroup)
const childrenGroupResult = await this._childrenGroupService.update(childrenGroup)

// 4. If everything succeeds, it returns the data of the created group.
return Promise.resolve(childrenGroupResult)
Expand Down
Loading

0 comments on commit b7bb403

Please sign in to comment.