Skip to content

Commit

Permalink
Merge branch 'hotfix/2.0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Nov 20, 2019
2 parents 2508e8f + d5077cd commit 55b2834
Show file tree
Hide file tree
Showing 37 changed files with 375 additions and 417 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "account",
"version": "2.0.6",
"version": "2.0.7",
"description": "Microservice responsible for user management and authentication on the OCARIoT platform.",
"main": "dist/server.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/application/domain/model/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Child extends User implements IJSONSerializable, IJSONDeserializabl
}

public fromJSON(json: any): Child {
if (!json) return this
if (!json || json === 'null') return this
super.fromJSON(json)

if (typeof json === 'string') {
Expand All @@ -82,6 +82,7 @@ export class Child extends User implements IJSONSerializable, IJSONDeserializabl
}
if (json.gender !== undefined) this.gender = json.gender
if (json.age !== undefined) this.age = json.age
if (json.last_sync !== undefined) this.last_sync = json.last_sync

return this
}
Expand Down
13 changes: 6 additions & 7 deletions src/application/domain/model/children.group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ChildrenGroup extends Entity implements IJSONSerializable, IJSONDes
}

set children(value: Array<Child> | undefined) {
this._children = value ? this.removesRepeatedChildren(value) : value
this._children = value && value instanceof Array ? this.removesRepeatedChildren(value) : value
}

get school_class(): string | undefined {
Expand Down Expand Up @@ -79,8 +79,9 @@ export class ChildrenGroup extends Entity implements IJSONSerializable, IJSONDes
if (json.id !== undefined) super.id = json.id
if (json.name !== undefined) this.name = json.name
if (json.school_class !== undefined) this.school_class = json.school_class
if (json.children !== undefined && json.children instanceof Array) {
this.children = json.children.map(child => new Child().fromJSON(child))
if (json.children !== undefined) {
if (json.children instanceof Array) this.children = json.children.map(child => new Child().fromJSON(child))
else this.children = json.children
}

return this
Expand All @@ -92,11 +93,9 @@ export class ChildrenGroup extends Entity implements IJSONSerializable, IJSONDes
name: this.name,
children: this.children ?
this.children.map(child => {
child.toJSON()
child.type = undefined
return child
}) :
this.children,
return child.toJSON()
}) : this.children,
school_class: this.school_class
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/application/domain/model/family.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Family extends User implements IJSONSerializable, IJSONDeserializab
}

set children(value: Array<Child> | undefined) {
this._children = value ? this.removesRepeatedChildren(value) : value
this._children = value && value instanceof Array ? this.removesRepeatedChildren(value) : value
}

public addChild(child: Child): void {
Expand All @@ -75,8 +75,9 @@ export class Family extends User implements IJSONSerializable, IJSONDeserializab
json = JSON.parse(json)
}

if (json.children !== undefined && json.children instanceof Array) {
this.children = json.children.map(child => new Child().fromJSON(child))
if (json.children !== undefined) {
if (json.children instanceof Array) this.children = json.children.map(child => new Child().fromJSON(child))
else this.children = json.children
}

return this
Expand Down
2 changes: 1 addition & 1 deletion src/application/domain/model/institution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Institution extends Entity implements IJSONSerializable, IJSONDeser
}

if (json.institution_id !== undefined) {
super.id = json.institution_id ? json.institution_id : undefined
super.id = json.institution_id ? json.institution_id : ''
return this
}

Expand Down
13 changes: 1 addition & 12 deletions src/application/domain/model/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ export class User extends Entity implements IJSONSerializable, IJSONDeserializab
this._scopes = value
}

public addScope(scope: string): void {
if (!this.scopes) this._scopes = []
if (scope) this._scopes.push(scope)
}

public removeScope(scope: string): void {
if (scope) {
this.scopes = this.scopes.filter(item => item !== scope)
}
}

public fromJSON(json: any): User {
if (!json) return this
if (typeof json === 'string' && JsonUtils.isJsonString(json)) {
Expand All @@ -96,7 +85,7 @@ export class User extends Entity implements IJSONSerializable, IJSONDeserializab
} else if (json.institution_id !== undefined) {
this.institution = new Institution().fromJSON(json)
}
if (json.scope !== undefined) this.scopes = json.scope
if (json.last_login !== undefined) this.last_login = json.last_login

return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/application/domain/validator/create.child.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CreateChildValidator {
fields.push(err.description.split(','))
}

if (!child.gender) fields.push('gender')
if (child.gender === undefined) fields.push('gender')
else if (!genders.includes(child.gender)) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
`The names of the allowed genders are: ${genders.join(', ')}.`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ export class CreateChildrenGroupValidator {
if (!childrenGroup.user || !childrenGroup.user.id) fields.push('user')
else ObjectIdValidator.validate(childrenGroup.user.id)

if (childrenGroup.children !== undefined && !(childrenGroup.children instanceof Array)) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY))
}

if (!childrenGroup.children || !childrenGroup.children.length) {
fields.push('Collection with children IDs')
} else {
childrenGroup.children.forEach(child => {
if (!child.id) {
fields.push('Collection with children IDs (ID can not be empty)')
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID)
} else {
try {
ObjectIdValidator.validate(child.id)
Expand Down
8 changes: 7 additions & 1 deletion src/application/domain/validator/create.family.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export class CreateFamilyValidator {
fields.push(err.description.split(','))
}

if (family.children !== undefined && !(family.children instanceof Array)) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY))
}

if (!family.children || !family.children.length) {
fields.push('Collection with children IDs')
} else {
family.children.forEach(child => {
if (!child.id) {
fields.push('Collection with children IDs (ID can not be empty)')
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID)
} else {
try {
ObjectIdValidator.validate(child.id)
Expand Down
2 changes: 1 addition & 1 deletion src/application/domain/validator/create.user.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CreateUserValidator {
else StringValidator.validate(user.password, 'password')

if (!user.type) fields.push('type')
if (!user.institution || !user.institution.id) {
if (!user.institution || user.institution.id === undefined) {
if (user.type !== UserType.APPLICATION) fields.push('institution')
}
else ObjectIdValidator.validate(user.institution.id, Strings.INSTITUTION.PARAM_ID_NOT_VALID_FORMAT)
Expand Down
2 changes: 1 addition & 1 deletion src/application/domain/validator/update.child.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class UpdateChildValidator {
throw err
}

if (child.gender && !genders.includes(child.gender)) {
if (child.gender !== undefined && !genders.includes(child.gender)) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
`The names of the allowed genders are: ${genders.join(', ')}.`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { StringValidator } from './string.validator'

export class UpdateChildrenGroupValidator {
public static validate(childrenGroup: ChildrenGroup): void | ValidationException {
const fields: Array<string> = []
const invalid_ids: Array<string> = []

if (childrenGroup.id) ObjectIdValidator.validate(childrenGroup.id, Strings.CHILDREN_GROUP.PARAM_ID_NOT_VALID_FORMAT)
Expand All @@ -16,10 +15,15 @@ export class UpdateChildrenGroupValidator {
if (childrenGroup.school_class !== undefined) {
StringValidator.validate(childrenGroup.school_class, 'school_class')
}
if (childrenGroup.children !== undefined && !(childrenGroup.children instanceof Array)) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY))
}
if (childrenGroup.children && childrenGroup.children.length > 0) {
childrenGroup.children.forEach(child => {
if (!child.id) {
fields.push('Collection with children IDs (ID can not be empty)')
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID)
} else {
try {
ObjectIdValidator.validate(child.id)
Expand All @@ -33,9 +37,6 @@ export class UpdateChildrenGroupValidator {
if (invalid_ids.length > 0) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
Strings.ERROR_MESSAGE.MULTIPLE_UUID_NOT_VALID_FORMAT.concat(invalid_ids.join(', ')))
} else if (fields.length > 0) {
throw new ValidationException(Strings.ERROR_MESSAGE.REQUIRED_FIELDS,
fields.join(', ').concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC))
}
}
}
12 changes: 7 additions & 5 deletions src/application/domain/validator/update.family.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Strings } from '../../../utils/strings'

export class UpdateFamilyValidator {
public static validate(family: Family): void | ValidationException {
const fields: Array<string> = []
const invalid_ids: Array<string> = []

try {
Expand All @@ -22,10 +21,16 @@ export class UpdateFamilyValidator {
throw err
}

if (family.children !== undefined && !(family.children instanceof Array)) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY))
}

if (family.children && family.children.length > 0) {
family.children.forEach(child => {
if (!child.id) {
fields.push('Collection with children IDs (ID can not be empty)')
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID)
} else {
try {
ObjectIdValidator.validate(child.id)
Expand All @@ -39,9 +44,6 @@ export class UpdateFamilyValidator {
if (invalid_ids.length > 0) {
throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS,
Strings.ERROR_MESSAGE.MULTIPLE_UUID_NOT_VALID_FORMAT.concat(invalid_ids.join(', ')))
} else if (fields.length > 0) {
throw new ValidationException(Strings.ERROR_MESSAGE.REQUIRED_FIELDS,
fields.join(', ').concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC))
}
}
}
4 changes: 2 additions & 2 deletions src/application/domain/validator/update.user.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export class UpdateUserValidator {
}
}
if (user.username !== undefined) StringValidator.validate(user.username, 'username')
if (user.institution && user.institution.id) {
if (user.institution && user.institution.id !== undefined) {
try {
ObjectIdValidator.validate(user.institution.id, Strings.INSTITUTION.PARAM_ID_NOT_VALID_FORMAT)
} catch (err) {
throw new ValidationException('INSTITUTION_ID_INVALID')
}
}
// validate parameters that can not be updated.
if (user.password) {
if (user.password !== undefined) {
throw new ValidationException('This parameter could not be updated.',
'A specific route to update user password already exists.' +
`Access: PATCH /users/${user.id}/password to update your password.`)
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/repository/base/base.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export abstract class BaseRepository<T extends Entity, TModel> implements IRepos
return new ValidationException('Invalid query parameters!')
}
}
return new RepositoryException(err && err.message ? err.message : '',
err && err.description ? err.description : '')
return new RepositoryException(err && err.message ? err.message : Strings.ERROR_MESSAGE.INTERNAL_SERVER_ERROR,
err && err.description ? err.description : undefined)
}

private findPopulate(q: any, populate?: any): Promise<Array<T>> {
Expand Down
1 change: 1 addition & 0 deletions src/ui/controller/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class ApplicationController {
try {
const application: Application = new Application().fromJSON(req.body)
application.id = undefined
application.last_login = undefined
const result: Application = await this._applicationService.add(application)
return res.status(HttpStatus.CREATED).send(this.toJSONView(result))
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions src/ui/controller/child.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class ChildController {
try {
const child: Child = new Child().fromJSON(req.body)
child.id = undefined
child.last_login = undefined
child.last_sync = undefined
const result: Child = await this._childService.add(child)
return res.status(HttpStatus.CREATED).send(this.toJSONView(result))
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/ui/controller/educator.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class EducatorController {
try {
const educator: Educator = new Educator().fromJSON(req.body)
educator.id = undefined
educator.last_login = undefined
const result: Educator = await this._educatorService.add(educator)
return res.status(HttpStatus.CREATED).send(this.toJSONView(result))
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/ui/controller/family.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class FamilyController {
try {
const family: Family = new Family().fromJSON(req.body)
family.id = undefined
family.last_login = undefined
const result: Family = await this._familyService.add(family)
return res.status(HttpStatus.CREATED).send(this.toJSONView(result))
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions src/ui/controller/health.professional.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class HealthProfessionalController {
try {
const healthProfessional: HealthProfessional = new HealthProfessional().fromJSON(req.body)
healthProfessional.id = undefined
healthProfessional.last_login = undefined
const result: HealthProfessional = await this._healthProfessionalService.add(healthProfessional)
return res.status(HttpStatus.CREATED).send(this.toJSONView(result))
} catch (err) {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ export abstract class Strings {
UUID_NOT_VALID_FORMAT: 'Some ID provided does not have a valid format!',
UUID_NOT_VALID_FORMAT_DESC: 'A 24-byte hex ID similar to this: 507f191e810c19729de860ea is expected.',
MULTIPLE_UUID_NOT_VALID_FORMAT: 'The following IDs from children attribute are not in valid format: ',
INVALID_MULTIPLE_UUID: 'Children field contains invalid IDs. It is expected that each item in the array is a ' +
'24-byte hex string like this: 507f191e810c19729de860ea',
INTERNAL_SERVER_ERROR: 'An internal server error has occurred.',
REQUIRED_FIELDS: 'Required fields were not provided...',
REQUIRED_FIELDS_DESC: ' are required!',
INVALID_FIELDS: 'One or more request fields are invalid...',
INVALID_STRING: ' must be a string!',
EMPTY_STRING: ' must have at least one character!',
INVALID_DATE: ', is not in valid ISO 8601 format.',
INVALID_DATE_DESC: 'Date must be in the format: yyyy-MM-dd\'T\'HH:mm:ssZ'
INVALID_DATE_DESC: 'Date must be in the format: yyyy-MM-dd\'T\'HH:mm:ssZ',
INVALID_ARRAY: ' must be an array!',
}
}
Loading

0 comments on commit 55b2834

Please sign in to comment.