diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2b7020d..12914bc 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -18,10 +18,6 @@ tags: description: "Users registered in the system" - name: "Users Skills" description: "Skills of users" -- name: "Skills" - description: "Skills registered in the system" -- name: "Skills Provider" - description: "Skill providers registered in the system" - name: "Roles" description: "Roles registered in the system" - name: "User Roles" @@ -815,558 +811,6 @@ paths: security: - Bearer: [] x-swagger-router-controller: "UsersSkills" - /skills: - get: - tags: - - "Skills" - description: "Get list of skills in the application. If no results, then empty\ - \ array is returned. Multiple filters are\nsupported.\n\n**Security** - Note\ - \ that for non-admin users, this endpoint will only return entities that\n\ - the user has created.\n" - operationId: "skillsGET" - parameters: - - $ref: '#/parameters/page' - - $ref: '#/parameters/perPage' - - name: "skillProviderId" - in: "query" - description: "The referenced skill provider id" - type: "string" - format: "UUID" - - name: "externalId" - in: "query" - description: "The external id of the skill" - type: "string" - - name: "name" - in: "query" - description: "The name of the skill" - type: "string" - - name: "orderBy" - in: "query" - type: "string" - description: "Specify by which field to sort by. Sorts in ascending order only" - responses: - "200": - description: "OK - the request was successful" - schema: - type: "array" - items: - $ref: "#/definitions/Skill" - headers: - X-Next-Page: - type: integer - description: The index of the next page - X-Page: - type: integer - description: The index of the current page (starting at 1) - X-Per-Page: - type: integer - description: The number of items to list per page - X-Prev-Page: - type: integer - description: The index of the previous page - X-Total: - type: integer - description: The total number of items - X-Total-Pages: - type: integer - description: The total number of pages - Link: - type: string - description: Pagination link header. - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - head: - tags: - - "Skills" - description: "Retrieve header information for get operation on Skills in the\ - \ application.\n\n**Security** - Note that for non-admin users, this endpoint\ - \ will only return entities that\nthe user has created.\n" - operationId: "skillsHEAD" - parameters: - - $ref: '#/parameters/page' - - $ref: '#/parameters/perPage' - - name: "skillProviderId" - in: "query" - description: "The referenced skill provider id" - type: "string" - format: "UUID" - - name: "externalId" - in: "query" - description: "The external id of the skill" - type: "string" - responses: - "200": - description: "Success response" - headers: - X-Next-Page: - type: integer - description: The index of the next page - X-Page: - type: integer - description: The index of the current page (starting at 1) - X-Per-Page: - type: integer - description: The number of items to list per page - X-Prev-Page: - type: integer - description: The index of the previous page - X-Total: - type: integer - description: The total number of items - X-Total-Pages: - type: integer - description: The total number of pages - Link: - type: string - description: Pagination link header. - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - post: - tags: - - "Skills" - description: "Create a new Skill.\n\n**Security** - This endpoint is accessible\ - \ by all authenticated users. \n" - operationId: "skillsPOST" - parameters: - - in: "body" - name: "body" - required: true - schema: - $ref: "#/definitions/SkillRequestBody" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/Skill" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - /skills/{skillId}: - get: - tags: - - "Skills" - description: "Get Skill by given skill id.\n\n**Security** - Note that for non-admin\ - \ users, this endpoint will only return entities that\nthe user has created.\ - \ \n" - operationId: "skillsSkillIdGET" - parameters: - - name: "skillId" - in: "path" - description: "The skill id" - required: true - type: "string" - format: "UUID" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/Skill" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - head: - tags: - - "Skills" - description: "Retrieve header information for get operation on Skill by its\ - \ Id in the application.\n\n**Security** - Note that for non-admin users,\ - \ this endpoint will only return entities that\nthe user has created. \ - \ \n" - operationId: "skillsSkillIdHEAD" - parameters: - - name: "skillId" - in: "path" - description: "The user id" - required: true - type: "string" - format: "UUID" - responses: - "200": - description: "OK - the request was successful" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - delete: - tags: - - "Skills" - description: "Remove an existing skill with given id.\n\n**Security** - Note\ - \ that this endpoint is only available for admin users. \n" - operationId: "skillsSkillIdDELETE" - parameters: - - name: "skillId" - in: "path" - description: "The skill id" - required: true - type: "string" - format: "UUID" - responses: - "204": - description: "OK - the request was successful" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - patch: - tags: - - "Skills" - description: "Update an existing skill with given id.\n\n**Security** - Note\ - \ that for non-admin users, this endpoint will only allow updates on entities\ - \ that the\ncalling user has created.\n" - operationId: "skillsSkillIdPATCH" - parameters: - - name: "skillId" - in: "path" - description: "The skill id" - required: true - type: "string" - format: "UUID" - - in: "body" - name: "body" - required: true - schema: - $ref: "#/definitions/SkillUpdateRequestBody" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/Skill" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - /skillsProviders: - get: - tags: - - "Skills Provider" - description: "Search Skills Provider in the application. If no results, then\ - \ empty array is returned.\n\n**Security** - Note that for non-admin users,\ - \ this endpoint will only return entities that\nthe user has created.\n" - operationId: "skillsProvidersGET" - parameters: - - $ref: '#/parameters/page' - - $ref: '#/parameters/perPage' - - name: "name" - in: "query" - description: "Filter by provider name" - required: false - type: "string" - responses: - "200": - description: "OK - the request was successful" - schema: - type: "array" - items: - $ref: "#/definitions/SkillsProvider" - headers: - X-Next-Page: - type: integer - description: The index of the next page - X-Page: - type: integer - description: The index of the current page (starting at 1) - X-Per-Page: - type: integer - description: The number of items to list per page - X-Prev-Page: - type: integer - description: The index of the previous page - X-Total: - type: integer - description: The total number of items - X-Total-Pages: - type: integer - description: The total number of pages - Link: - type: string - description: Pagination link header. - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" - head: - tags: - - "Skills Provider" - description: "Retrieve header information for a search operation on skills providers\ - \ in the application.\n\n**Security** - Note that for non-admin users, this\ - \ endpoint will only return entities that\nthe user has created.\n" - operationId: "skillsProvidersHEAD" - parameters: - - name: "name" - in: "query" - description: "Filter by provider name" - required: false - type: "string" - responses: - "200": - description: "Success response" - headers: - X-Next-Page: - type: integer - description: The index of the next page - X-Page: - type: integer - description: The index of the current page (starting at 1) - X-Per-Page: - type: integer - description: The number of items to list per page - X-Prev-Page: - type: integer - description: The index of the previous page - X-Total: - type: integer - description: The total number of items - X-Total-Pages: - type: integer - description: The total number of pages - Link: - type: string - description: Pagination link header. - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" - post: - tags: - - "Skills Provider" - description: "Create a new Skills Provider.\n\n**Security** - This endpoint\ - \ is accessible by all authenticated users. \n" - operationId: "skillsProvidersPOST" - parameters: - - in: "body" - name: "body" - required: true - schema: - $ref: "#/definitions/NameRequestBody" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/SkillsProvider" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" - /skillsProviders/{providerId}: - get: - tags: - - "Skills Provider" - description: "Get skills provider with given id.\n\n**Security** - Note that\ - \ for non-admin users, this endpoint will only return entities that\nthe user\ - \ has created. \n" - operationId: "skillsProvidersProviderIdGET" - parameters: - - name: "providerId" - in: "path" - description: "The provider id" - required: true - type: "string" - format: "UUID" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/SkillsProvider" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" - head: - tags: - - "Skills Provider" - description: "Get skills provider with given id, but only header information\ - \ is returned.\n\n**Security** - Note that for non-admin users, this endpoint\ - \ will only return entities that\nthe user has created. \n" - operationId: "skillsProvidersProviderIdHEAD" - parameters: - - name: "providerId" - in: "path" - description: "The provider id" - required: true - type: "string" - format: "UUID" - responses: - "200": - description: "OK - the request was successful" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" - delete: - tags: - - "Skills Provider" - description: "Remove an existing skills provider with given id.\n\n**Security**\ - \ - Note that this endpoint is only available for admin users. \n" - operationId: "skillsProvidersProviderIdDELETE" - parameters: - - name: "providerId" - in: "path" - description: "The provider id" - required: true - type: "string" - format: "UUID" - responses: - "204": - description: "OK - the request was successful" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" - patch: - tags: - - "Skills Provider" - description: "Update an existing skills provider with given id.\n\n**Security**\ - \ - Note that for non-admin users, this endpoint will only allow updates on\ - \ entities that the\ncalling user has created.\n" - operationId: "skillsProvidersProviderIdPATCH" - parameters: - - name: "providerId" - in: "path" - description: "The provider id" - required: true - type: "string" - format: "UUID" - - in: "body" - name: "body" - required: true - schema: - $ref: "#/definitions/NameRequestBody" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/SkillsProvider" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "SkillsProvider" /roles: get: tags: @@ -4618,34 +4062,6 @@ definitions: type: "string" description: "The name of the provider." - $ref: "#/definitions/AuditFields" - Skill: - allOf: - - type: "object" - required: - - "externalId" - - "id" - - "name" - - "skillProviderId" - - "uri" - properties: - id: - type: "string" - format: "UUID" - description: "The skill id" - skillProviderId: - type: "string" - format: "UUID" - description: "The referenced skill provider id" - name: - type: "string" - description: "The name of the skill" - externalId: - type: "string" - description: "The external id for the skill" - uri: - type: "string" - description: "The uri for the skill" - - $ref: "#/definitions/AuditFields" LookupSkill: allOf: - type: "object" @@ -4661,46 +4077,6 @@ definitions: name: type: "string" description: "The name of the skill" - SkillRequestBody: - allOf: - - type: "object" - - $ref: "#/definitions/SkillUpdateRequestBody" - SkillUpdateRequestBody: - type: "object" - properties: - skillProviderId: - type: "string" - format: "UUID" - description: "The id of provider for this Skill." - name: - type: "string" - description: "Name of Skill" - uri: - type: "string" - description: "Uri of Skill" - externalId: - type: "string" - description: "External Id of skill" - example: - skillProviderId: "skillProviderId" - name: "name" - externalId: "externalId" - uri: "uri" - SkillsProvider: - allOf: - - type: "object" - required: - - "id" - - "name" - properties: - id: - type: "string" - format: "UUID" - description: "The id of the provider." - name: - type: "string" - description: "The name of the provider." - - $ref: "#/definitions/AuditFields" ExternalProfile: allOf: - type: "object" diff --git a/src/modules/skill/controller.js b/src/modules/skill/controller.js deleted file mode 100644 index e42c58f..0000000 --- a/src/modules/skill/controller.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * the skill controller - */ - -const service = require('./service') -const helper = require('../../common/helper') -const methods = helper.getControllerMethods(service) - -module.exports = { - ...methods -} diff --git a/src/modules/skill/route.js b/src/modules/skill/route.js deleted file mode 100644 index b8c406a..0000000 --- a/src/modules/skill/route.js +++ /dev/null @@ -1,54 +0,0 @@ -/** - * the skill routes - */ - -const Controller = require('./controller') -const consts = require('../../consts') -module.exports = { - '/skills': { - get: { - method: Controller.search, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skill', 'all:skill'] - }, - post: { - method: Controller.create, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['create:skill', 'all:skill'] - }, - head: { - method: Controller.search, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skill', 'all:skill'] - } - }, - '/skills/:id': { - get: { - method: Controller.get, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skill', 'all:skill'] - }, - head: { - method: Controller.get, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skill', 'all:skill'] - }, - patch: { - method: Controller.patch, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['update:skill', 'all:skill'] - }, - delete: { - method: Controller.remove, - auth: 'jwt', - access: [...consts.AdminUser, consts.UserRoles.ubahn], - scopes: ['delete:skill', 'all:skill'] - } - } -} diff --git a/src/modules/skill/service.js b/src/modules/skill/service.js deleted file mode 100644 index cfccdcb..0000000 --- a/src/modules/skill/service.js +++ /dev/null @@ -1,157 +0,0 @@ -/** - * the skill services - */ - -const joi = require('@hapi/joi') -const _ = require('lodash') - -const errors = require('../../common/errors') -const helper = require('../../common/helper') -const dbHelper = require('../../common/db-helper') -const serviceHelper = require('../../common/service-helper') -const sequelize = require('../../models/index') - -const Skill = sequelize.models.Skill -const SkillsProvider = sequelize.models.SkillsProvider -const UsersSkill = sequelize.models.UsersSkill -const resource = serviceHelper.getResource('Skill') -const uniqueFields = [['skillProviderId', 'externalId', 'name']] - -/** - * create entity - * @param entity the request device entity - * @param auth the auth information - * @return {Promise} the created device - */ -async function create (entity, auth) { - await dbHelper.get(SkillsProvider, entity.skillProviderId) - await dbHelper.makeSureUnique(Skill, entity, uniqueFields) - - const result = await dbHelper.create(Skill, entity, auth) - await serviceHelper.createRecordInEs(resource, result.dataValues) - - return result -} - -create.schema = { - entity: { - skillProviderId: joi.string().required(), - name: joi.string().required(), - uri: joi.string(), - externalId: joi.string() - }, - auth: joi.object() -} - -/** - * patch device by id - * @param id the device id - * @param entity the request device entity - * @param auth the auth object - * @param params the query params - * @return {Promise} the updated device - */ -async function patch (id, entity, auth, params) { - if (entity.skillProviderId) { - await dbHelper.get(SkillsProvider, entity.skillProviderId) - } - await dbHelper.makeSureUnique(Skill, entity, uniqueFields) - - const newEntity = await dbHelper.update(Skill, id, entity, auth) - await serviceHelper.patchRecordInEs(resource, newEntity.dataValues) - - return newEntity -} - -patch.schema = { - id: joi.string(), - entity: { - skillProviderId: joi.string(), - name: joi.string(), - uri: joi.string(), - externalId: joi.string() - }, - auth: joi.object(), - params: joi.object() -} - -/** - * get device by id - * @param id the device id - * @param auth the auth obj - * @param params the path parameters - * @param query the query parameters - * @param fromDb Should we bypass Elasticsearch for the record and fetch from db instead? - * @return {Promise} the db device - */ -async function get (id, auth, params, query = {}, fromDb = false) { - const trueParams = _.assign(params, query) - if (!fromDb) { - const esResult = await serviceHelper.getRecordInEs(resource, id, trueParams, auth) - if (esResult) { - return esResult - } - } - - const recordObj = await dbHelper.get(Skill, id) - if (!recordObj) { - throw errors.newEntityNotFoundError(`cannot find ${Skill.name} where ${_.map(trueParams, (v, k) => `${k}:${v}`).join(', ')}`) - } - - helper.permissionCheck(auth, recordObj) - return recordObj -} - -/** - * search devices by query - * @param query the search query - * @param auth the auth object - * @return {Promise} the results - */ -async function search (query, auth) { - // get from elasticsearch, if that fails get from db - // and response headers ('X-Total', 'X-Page', etc.) are not set in case of db return - const esResult = await serviceHelper.searchRecordInEs(resource, query, auth) - if (esResult) { - return esResult - } - - const items = await dbHelper.find(Skill, query, auth) - return { fromDb: true, result: items, total: items.length } -} - -search.schema = { - query: { - page: joi.id(), - perPage: joi.pageSize(), - skillProviderId: joi.string(), - name: joi.string(), - externalId: joi.string(), - orderBy: joi.string() - }, - auth: joi.object() -} - -/** - * remove entity by id - * @param id the entity id - * @param auth the auth object - * @param params the query params - * @return {Promise} no data returned - */ -async function remove (id, auth, params) { - const existing = await dbHelper.find(UsersSkill, { skillId: id }) - if (existing.length > 0) { - throw errors.deleteConflictError(`Please delete ${UsersSkill.name} with ids ${existing.map(o => o.id)}`) - } - await dbHelper.remove(Skill, id) - await serviceHelper.deleteRecordFromEs(id, params, resource) -} - -module.exports = { - create, - search, - patch, - get, - remove -} diff --git a/src/modules/skillsProvider/controller.js b/src/modules/skillsProvider/controller.js deleted file mode 100644 index 4a728ec..0000000 --- a/src/modules/skillsProvider/controller.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * the skillsProvider controller - */ - -const service = require('./service') -const helper = require('../../common/helper') -const methods = helper.getControllerMethods(service) - -module.exports = { - ...methods -} diff --git a/src/modules/skillsProvider/route.js b/src/modules/skillsProvider/route.js deleted file mode 100644 index 15c0045..0000000 --- a/src/modules/skillsProvider/route.js +++ /dev/null @@ -1,54 +0,0 @@ -/** - * the skillsProvider routes - */ - -const Controller = require('./controller') -const consts = require('../../consts') -module.exports = { - '/skillsProviders': { - get: { - method: Controller.search, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skillsProvider', 'all:skillsProvider'] - }, - post: { - method: Controller.create, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['create:skillsProvider', 'all:skillsProvider'] - }, - head: { - method: Controller.search, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skillsProvider', 'all:skillsProvider'] - } - }, - '/skillsProviders/:id': { - get: { - method: Controller.get, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skillsProvider', 'all:skillsProvider'] - }, - head: { - method: Controller.get, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['read:skillsProvider', 'all:skillsProvider'] - }, - patch: { - method: Controller.patch, - auth: 'jwt', - access: consts.AllAuthenticatedUsers, - scopes: ['update:skillsProvider', 'all:skillsProvider'] - }, - delete: { - method: Controller.remove, - auth: 'jwt', - access: [...consts.AdminUser, consts.UserRoles.ubahn], - scopes: ['delete:skillsProvider', 'all:skillsProvider'] - } - } -} diff --git a/src/modules/skillsProvider/service.js b/src/modules/skillsProvider/service.js deleted file mode 100644 index 2e05452..0000000 --- a/src/modules/skillsProvider/service.js +++ /dev/null @@ -1,142 +0,0 @@ -/** - * the skillsProvider services - */ - -const joi = require('@hapi/joi') -const _ = require('lodash') - -const errors = require('../../common/errors') -const helper = require('../../common/helper') -const dbHelper = require('../../common/db-helper') -const serviceHelper = require('../../common/service-helper') -const sequelize = require('../../models/index') - -const SkillsProvider = sequelize.models.SkillsProvider -const Skill = sequelize.models.Skill -const OrganizationSkillsProvider = sequelize.models.OrganizationSkillsProvider -const resource = serviceHelper.getResource('SkillsProvider') - -/** - * create entity - * @param entity the request device entity - * @param auth the auth information - * @return {Promise} the created device - */ -async function create (entity, auth) { - const result = await dbHelper.create(SkillsProvider, entity, auth) - await serviceHelper.createRecordInEs(resource, result.dataValues) - return result -} - -create.schema = { - entity: { - name: joi.string().required() - }, - auth: joi.object() -} - -/** - * patch device by id - * @param id the device id - * @param entity the request device entity - * @param auth the auth object - * @param params the query params - * @return {Promise} the updated device - */ -async function patch (id, entity, auth, params) { - const newEntity = await dbHelper.update(SkillsProvider, id, entity, auth) - await serviceHelper.patchRecordInEs(resource, newEntity.dataValues) - return newEntity -} - -patch.schema = { - id: joi.string(), - entity: { - name: joi.string() - }, - auth: joi.object(), - params: joi.object() -} - -/** - * get device by id - * @param id the device id - * @param auth the auth obj - * @param params the path parameters - * @param query the query parameters - * @param fromDb Should we bypass Elasticsearch for the record and fetch from db instead? - * @return {Promise} the db device - */ -async function get (id, auth, params, query = {}, fromDb = false) { - const trueParams = _.assign(params, query) - if (!fromDb) { - const esResult = await serviceHelper.getRecordInEs(resource, id, trueParams, auth) - if (esResult) { - return esResult - } - } - - const recordObj = await dbHelper.get(SkillsProvider, id) - if (!recordObj) { - throw errors.newEntityNotFoundError(`cannot find ${SkillsProvider.name} where ${_.map(trueParams, (v, k) => `${k}:${v}`).join(', ')}`) - } - - helper.permissionCheck(auth, recordObj) - return recordObj -} - -/** - * search devices by query - * @param query the search query - * @param auth the auth object - * @return {Promise} the results - */ -async function search (query, auth) { - // get from elasticsearch, if that fails get from db - // and response headers ('X-Total', 'X-Page', etc.) are not set in case of db return - const esResult = await serviceHelper.searchRecordInEs(resource, query, auth) - if (esResult) { - return esResult - } - - const items = await dbHelper.find(SkillsProvider, query, auth) - return { fromDb: true, result: items, total: items.length } -} - -search.schema = { - query: { - page: joi.id(), - perPage: joi.pageSize(), - name: joi.string() - }, - auth: joi.object() -} - -/** - * remove entity by id - * @param id the entity id - * @param auth the auth object - * @param params the query params - * @return {Promise} no data returned - */ -async function remove (id, auth, params) { - let existing = await dbHelper.find(Skill, { skillProviderId: id }) - if (existing.length > 0) { - throw errors.deleteConflictError(`Please delete ${Skill.name} with ids ${existing.map(o => o.id)}`) - } - existing = await dbHelper.find(OrganizationSkillsProvider, { skillProviderId: id }) - if (existing.length > 0) { - throw errors.deleteConflictError(`Please delete ${OrganizationSkillsProvider.name} with ids ${existing.map(o => o.id)}`) - } - - await dbHelper.remove(SkillsProvider, id) - await serviceHelper.deleteRecordFromEs(id, params, resource) -} - -module.exports = { - create, - search, - patch, - get, - remove -}