UNPKG

@defra/wls-eps-api

Version:

The API to support the wildlife licencing of European Protected Species

42 lines (35 loc) 1.26 kB
/* * Create the application user object and return 201 */ import { v4 as uuidv4 } from 'uuid' import { models } from '../../model/sequentelize-model.js' import { APPLICATION_JSON } from '../../constants.js' import { cache } from '../../services/cache.js' import { clearCaches } from './application-cache.js' import { prepareResponse } from './application-proc.js' export default async (context, req, h) => { try { const { userId } = context.request.params const user = await models.users.findByPk(userId) // Check the user exists if (!user) { return h.response().code(404) } await clearCaches(userId) const applicationPayload = (({ sddsId, ...l }) => l)(req.payload || {}) const { dataValues } = await models.applications.create({ id: uuidv4(), userId: userId, sddsId: req.payload?.sddsId ?? null, application: applicationPayload }) const responseBody = prepareResponse(dataValues) await cache.save(`/user/${dataValues.userId}/application/${dataValues.id}`, responseBody) return h.response(responseBody) .type(APPLICATION_JSON) .code(201) } catch (err) { console.error('Error inserting into APPLICATIONS table', err) throw new Error(err.message) } }