|
| 1 | +import UserBehaviour from "../../models/userBehaviour.model"; |
| 2 | +import { |
| 3 | + IUserBehaviourService, |
| 4 | + UserBehaviourRequestDTO, |
| 5 | + UserBehaviourResponseDTO, |
| 6 | +} from "../interfaces/userBehaviourService"; |
| 7 | +import { getErrorMessage } from "../../utilities/errorUtils"; |
| 8 | +import logger from "../../utilities/logger"; |
| 9 | + |
| 10 | +const Logger = logger(__filename); |
| 11 | + |
| 12 | +class UserBehaviourService implements IUserBehaviourService { |
| 13 | + /* eslint-disable class-methods-use-this */ |
| 14 | + async getEntity(id: string): Promise<UserBehaviourResponseDTO> { |
| 15 | + let userBehaviour: UserBehaviour | null; |
| 16 | + try { |
| 17 | + userBehaviour = await UserBehaviour.findByPk(id, { raw: true }); |
| 18 | + if (!userBehaviour) { |
| 19 | + throw new Error(`Entity id ${id} not found`); |
| 20 | + } |
| 21 | + } catch (error: unknown) { |
| 22 | + Logger.error(`Failed to get entity. Reason = ${getErrorMessage(error)}`); |
| 23 | + throw error; |
| 24 | + } |
| 25 | + |
| 26 | + return { |
| 27 | + behaviour_id: String(userBehaviour.id), |
| 28 | + level: userBehaviour.level, |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + async getEntities(): Promise<UserBehaviourResponseDTO[]> { // get |
| 33 | + try { |
| 34 | + const entities: Array<UserBehaviour> = await UserBehaviour.findAll({ |
| 35 | + raw: true, |
| 36 | + }); |
| 37 | + return entities.map((entity) => ({ |
| 38 | + id: String(entity.id), |
| 39 | + stringField: entity.string_field, |
| 40 | + intField: entity.int_field, |
| 41 | + enumField: entity.enum_field, |
| 42 | + stringArrayField: entity.string_array_field, |
| 43 | + boolField: entity.bool_field, |
| 44 | + })); |
| 45 | + } catch (error: unknown) { |
| 46 | + Logger.error( |
| 47 | + `Failed to get entities. Reason = ${getErrorMessage(error)}`, |
| 48 | + ); |
| 49 | + throw error; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + async createEntity( |
| 54 | + // post |
| 55 | + entity: UserBehaviourRequestDTO, |
| 56 | + ): Promise<UserBehaviourResponseDTO> { |
| 57 | + let newEntity: UserBehaviour | null; |
| 58 | + try { |
| 59 | + newEntity = await UserBehaviour.create({ |
| 60 | + string_field: entity.stringField, |
| 61 | + int_field: entity.intField, |
| 62 | + enum_field: entity.enumField, |
| 63 | + string_array_field: entity.stringArrayField, |
| 64 | + bool_field: entity.boolField, |
| 65 | + }); |
| 66 | + } catch (error: unknown) { |
| 67 | + Logger.error( |
| 68 | + `Failed to create entity. Reason = ${getErrorMessage(error)}`, |
| 69 | + ); |
| 70 | + throw error; |
| 71 | + } |
| 72 | + return { |
| 73 | + behaviour_id: String(newEntity.id), |
| 74 | + level: newEntity.int_field, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + async updateEntity( // put |
| 79 | + id: string, |
| 80 | + entity: UserBehaviourRequestDTO, // ! check format of put |
| 81 | + ): Promise<UserBehaviourResponseDTO | null> { |
| 82 | + let resultingEntity: UserBehaviour | null; |
| 83 | + let updateResult: [number, UserBehaviour[]] | null; |
| 84 | + try { |
| 85 | + updateResult = await PgSimpleEntity.update( |
| 86 | + { |
| 87 | + string_field: entity.stringField, |
| 88 | + int_field: entity.intField, |
| 89 | + enum_field: entity.enumField, |
| 90 | + string_array_field: entity.stringArrayField, |
| 91 | + bool_field: entity.boolField, |
| 92 | + }, |
| 93 | + { where: { id }, returning: true }, |
| 94 | + ); |
| 95 | + |
| 96 | + if (!updateResult[0]) { |
| 97 | + throw new Error(`Entity id ${id} not found`); |
| 98 | + } |
| 99 | + [, [resultingEntity]] = updateResult; |
| 100 | + } catch (error: unknown) { |
| 101 | + Logger.error( |
| 102 | + `Failed to update entity. Reason = ${getErrorMessage(error)}`, |
| 103 | + ); |
| 104 | + throw error; |
| 105 | + } |
| 106 | + return { |
| 107 | + id: String(resultingEntity.id), |
| 108 | + stringField: resultingEntity.string_field, |
| 109 | + intField: resultingEntity.int_field, |
| 110 | + enumField: resultingEntity.enum_field, |
| 111 | + stringArrayField: resultingEntity.string_array_field, |
| 112 | + boolField: resultingEntity.bool_field, |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + async deleteEntity(id: string): Promise<string> { |
| 117 | + try { |
| 118 | + const deleteResult: number | null = await UserBehaviour.destroy({ |
| 119 | + where: { id }, |
| 120 | + }); |
| 121 | + if (!deleteResult) { |
| 122 | + throw new Error(`Entity id ${id} not found`); |
| 123 | + } |
| 124 | + return id; |
| 125 | + } catch (error: unknown) { |
| 126 | + Logger.error( |
| 127 | + `Failed to delete entity. Reason = ${getErrorMessage(error)}`, |
| 128 | + ); |
| 129 | + throw error; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export default UserBehaviourService; |
0 commit comments