# Use localization objects for all API responses
## TODO
- create full list of errors. Need to be aprroved
- create script to insert all data in localizations
## Changes in DB
Collection: localizations
```
{
_id: mongoOjectId,
languages: [{ code: string, text: string }],
errorCode: { type: number, unique: true },
systemReference: { type: string, unique: true }
}
```
| field | description |
| ----- | ----------- |
| errorCode | we need to have this field for use this value in responses
| systemReference | this field is needed for generating enum from DB. The same functionality that we we has on system events
## Redis cache
Key value to get by localizationId errorCode
```
localization_errorCode: {
[localizationMongoId]: { errorCode: number },
}
```
## Feature
Fields field in query -- approved
## Standart API responses
| method | status code | response format | description |
| ------ | ----------- | --------------- | ----------- |
| POST,PUT, PATCH,DELETE,GET | 200,201,207, | ```{ message, data: Object, localizationId }``` | ~~return object what was created or array of objects~~ <br>message - localized message<br>localizationId - localization mongo id value<br>data -- result. Always Object. If result has array of data then inside data we will have some field(not data). <br> Example: { data: { tasks: [{}], total: 1}, message: 'dfdgfd', localizationId: 'dsgdfhfd' }
| * | 400 | {<br> message: string,<br> localizationId: string,<br> code: number,<br> data: {}<br>} | message - localized error message. For localization we will get default language from system configuration<br><br>localizationId - localization mongo id value<br><br>code - unique error code<br><br>data - additional data what can be used for UI message render. For example: data: { workspaceId: 1, action: 'disable' }, then we has in DB message: `Can't apply action {{action}} on workspace with id {{workspacceId}}`. In this case we can correct render this error message on other languages.
| * | 401 | {<br> message: string,<br> localizationId: string,<br>} | This response we returned in case when auth token are expired, not exists in DB
| * | 403 | {<br> message: string,<br> localizationId: string,<br> systemFunctionId: number<br>} | This response we returned in the case when auth token does not allow using API method
| * | 404 | {<br> message: string,<br> localizationId: string<br>} | returned only in case when route was not found
| * | 500 | {<br> message: string,<br> requestId: string,<br> localizationId: string<br>} | This response will be returned only in cases when we have some unhandled errors. Add rake direct send message in channel
## Code changes
```
before:
return res.status(400).send({ message: string, code?: number });
now:
return errorResponse(res, localizationMongoId, { [propertyName]: value })
Examples:
return res.status(400).send({ message: 'Field entityId was not found', code: 56});
return errorResponse(res, localizationEnum.http.missed, { field: 'entityId' })
return res.status(400).send({ message: 'Field workspaceId was not found', code: 56});
return errorResponse(res, localizationEnum.http.missed, { field: 'workspaceId' })
return res.status(400).send({ message: 'Field workspaceId has incorrect value format', code: 57});
return errorResponse(res, localizationEnum.http.typeMismatch, { field: 'workspaceId' })
```