| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 'use strict'
-
- const Joi = require('joi')
- const errorSchema = require('../../schemas/errors')
- const params = require('../../schemas/params')
-
- const pluginConfig = {
- handlerType: 'user',
- docs: {
- description: 'Create profile for user',
- notes: 'Create a profile associated with this user',
- },
- }
-
- const validators = {
- /** Validate the header (cookie check) */
- // headers: true,
-
- /** Validate the route params (/active/{thing}) */
- params: params.userId,
-
- /** Validate the route query (/active/{thing}?limit=10&offset=10) */
- // query: true,
- /** Validate the incoming payload (POST method) */
- payload: Joi.array().items(
- Joi.object({
- response_key_id: Joi.number().required(),
- val: Joi.string().required(),
- }),
- ),
- }
-
- const responseSchemas = {
- response: Joi.object({
- profile_id: Joi.number(),
- user_id: Joi.number(),
- user_name: Joi.string(),
- }).label('created_profile'),
- error: errorSchema.single,
- }
-
- module.exports = {
- method: 'POST',
- path: '/{user_id}/profile',
- options: {
- ...pluginConfig.docs,
- tags: ['api'],
- /** Protect this route with authentication? */
- auth: false,
- cors: true,
- handler: async function (request, h) {
- const { userService, profileService } = request.server.services()
- const userId = request.params.user_id
- const user = await userService.findById(userId)
- const type = user.is_poster == 1 ? 'poster' : 'seeker'
-
- const profiles = await profileService.getCompleteProfilesFor(
- userId,
- type,
- )
-
- try {
- if (type === 'seeker' && profiles.length > 0) {
- throw new RangeError(
- 'Job seekers may only have ONE profile',
- )
- }
- /** Grab payload info */
- const res = request.payload
- /** Don't log password in response table */
- const resWithoutPass = res.filter(r => {
- return r.response_key_id !== 9
- })
- const profile =
- await profileService.saveResponsesCreateProfileFor(
- userId,
- resWithoutPass,
- )
- return h
- .response({
- ok: true,
- handler: pluginConfig.handlerType,
- data: profile,
- })
- .code(201)
- } 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: {
- 201: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.response,
- }).label('created_profile_res'),
- 409: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.error,
- }).label('error_single_res'),
- },
- },
- },
- }
|