const Joi = require('joi') const apiSchema = require('../../schemas/api') const errorSchema = require('../../schemas/errors') const pluginConfig = { handlerType: 'reveal', docs: { description: 'reveal', notes: 'Reveal profile information to a grouping by membership', }, opts: { tags: ['api'], auth: { strategy: 'default_jwt' }, cors: true, }, } const validators = { params: Joi.object({ grouping_id: Joi.number() }), query: Joi.object({ profile_id: Joi.number(), tag_id: Joi.number() }), } const responseSchemas = { response: Joi.object({ tags: Joi.array().items(), }), error: errorSchema.single, } module.exports = { method: 'POST', path: '/{grouping_id}/reveal', options: { ...pluginConfig.docs, ...pluginConfig.opts, handler: async function (request, h) { const { membershipService, profileService } = request.server.services() const grouping_id = request.params.grouping_id const { profile_id, tag_id } = request.query try { const tags = await profileService.revealProfileInfo({ profile_id, grouping_id, tag_id, is_deleted: false, }) // Notify both profiles that information has been revealed const memberships = await membershipService.findMemberships([ grouping_id, ]) const idsInGroup = memberships.map( membership => membership.profile_id, ) idsInGroup.forEach(profile_id => { request.server.methods.notify( `${profile_id}.stonk`, { name: 'REVEALED INFO', tag: tag_id, type: 'info', }, h, ) }) return h .response({ ok: true, handler: pluginConfig.handlerType, data: { tags }, }) .code(200) } catch (err) { return h .response({ ok: false, handler: pluginConfig.handlerType, data: { error: `${err}` }, }) .code(409) } }, /** Validate based on validators object */ validate: { ...validators, failAction: 'log', }, /** Validate the server response */ response: { status: { 200: apiSchema.single .append({ data: responseSchemas.response, }) .label('reveal_res'), 409: apiSchema.single .append({ data: responseSchemas.error, }) .label('error_single_res'), }, }, }, }