|
| 1 | +import { Errors, mapErrorDetails, sanitizeErrorMessage } from "../util.js"; |
| 2 | +import { v4 as uuid } from "uuid"; |
| 3 | +import Joi from "joi"; |
| 4 | + |
| 5 | +const idSchema = Joi.string().guid({ |
| 6 | + version: "uuidv4", |
| 7 | +}); |
| 8 | + |
| 9 | +const todoSchema = Joi.object({ |
| 10 | + id: idSchema.alter({ |
| 11 | + create: (schema) => schema.forbidden(), |
| 12 | + update: (schema) => schema.required(), |
| 13 | + }), |
| 14 | + title: Joi.string().max(256).required(), |
| 15 | + completed: Joi.boolean().required(), |
| 16 | +}); |
| 17 | + |
| 18 | +export default function (components) { |
| 19 | + const { todoRepository } = components; |
| 20 | + return { |
| 21 | + createTodo: async function (payload, callback) { |
| 22 | + const socket = this; |
| 23 | + |
| 24 | + // validate the payload |
| 25 | + const { error, value } = todoSchema.tailor("create").validate(payload, { |
| 26 | + abortEarly: false, |
| 27 | + stripUnknown: true, |
| 28 | + }); |
| 29 | + |
| 30 | + if (error) { |
| 31 | + return callback({ |
| 32 | + error: Errors.INVALID_PAYLOAD, |
| 33 | + errorDetails: mapErrorDetails(error.details), |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + value.id = uuid(); |
| 38 | + |
| 39 | + // persist the entity |
| 40 | + try { |
| 41 | + await todoRepository.save(value); |
| 42 | + } catch (e) { |
| 43 | + return callback({ |
| 44 | + error: sanitizeErrorMessage(e), |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + // acknowledge the creation |
| 49 | + callback({ |
| 50 | + data: value.id, |
| 51 | + }); |
| 52 | + |
| 53 | + // notify the other users |
| 54 | + socket.broadcast.emit("todo:created", value); |
| 55 | + }, |
| 56 | + |
| 57 | + readTodo: async function (id, callback) { |
| 58 | + const { error } = idSchema.validate(id); |
| 59 | + |
| 60 | + if (error) { |
| 61 | + return callback({ |
| 62 | + error: Errors.ENTITY_NOT_FOUND, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const todo = await todoRepository.findById(id); |
| 68 | + callback({ |
| 69 | + data: todo, |
| 70 | + }); |
| 71 | + } catch (e) { |
| 72 | + callback({ |
| 73 | + error: sanitizeErrorMessage(e), |
| 74 | + }); |
| 75 | + } |
| 76 | + }, |
| 77 | + |
| 78 | + updateTodo: async function (payload, callback) { |
| 79 | + const socket = this; |
| 80 | + |
| 81 | + const { error, value } = todoSchema.tailor("update").validate(payload, { |
| 82 | + abortEarly: false, |
| 83 | + stripUnknown: true, |
| 84 | + }); |
| 85 | + |
| 86 | + if (error) { |
| 87 | + return callback({ |
| 88 | + error: Errors.INVALID_PAYLOAD, |
| 89 | + errorDetails: mapErrorDetails(error.details), |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + await todoRepository.save(value); |
| 95 | + } catch (e) { |
| 96 | + return callback({ |
| 97 | + error: sanitizeErrorMessage(e), |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + callback(); |
| 102 | + socket.broadcast.emit("todo:updated", value); |
| 103 | + }, |
| 104 | + |
| 105 | + deleteTodo: async function (id, callback) { |
| 106 | + const socket = this; |
| 107 | + |
| 108 | + const { error } = idSchema.validate(id); |
| 109 | + |
| 110 | + if (error) { |
| 111 | + return callback({ |
| 112 | + error: Errors.ENTITY_NOT_FOUND, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + await todoRepository.deleteById(id); |
| 118 | + } catch (e) { |
| 119 | + return callback({ |
| 120 | + error: sanitizeErrorMessage(e), |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + callback(); |
| 125 | + socket.broadcast.emit("todo:deleted", id); |
| 126 | + }, |
| 127 | + |
| 128 | + listTodo: async function (callback) { |
| 129 | + try { |
| 130 | + callback({ |
| 131 | + data: await todoRepository.findAll(), |
| 132 | + }); |
| 133 | + } catch (e) { |
| 134 | + callback({ |
| 135 | + error: sanitizeErrorMessage(e), |
| 136 | + }); |
| 137 | + } |
| 138 | + }, |
| 139 | + }; |
| 140 | +} |
0 commit comments