2022-01-06 01:26:15 +00:00
|
|
|
import { Op } from "sequelize";
|
|
|
|
import * as Yup from "yup";
|
|
|
|
import AppError from "../../errors/AppError";
|
|
|
|
import Queue from "../../models/Queue";
|
|
|
|
import ShowQueueService from "./ShowQueueService";
|
2024-04-03 21:38:56 +00:00
|
|
|
import { set } from "../../helpers/RedisClient";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
interface QueueData {
|
|
|
|
name?: string;
|
|
|
|
color?: string;
|
|
|
|
greetingMessage?: string;
|
2024-04-12 21:33:15 +00:00
|
|
|
farewellMessage?: string;
|
2024-04-03 21:38:56 +00:00
|
|
|
cc?: string;
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const UpdateQueueService = async (
|
|
|
|
queueId: number | string,
|
|
|
|
queueData: QueueData
|
|
|
|
): Promise<Queue> => {
|
2023-04-20 18:48:42 +00:00
|
|
|
try {
|
|
|
|
const { color, name } = queueData;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
const queueSchema = Yup.object().shape({
|
|
|
|
name: Yup.string()
|
|
|
|
.min(2, "ERR_QUEUE_INVALID_NAME")
|
|
|
|
.test(
|
|
|
|
"Check-unique-name",
|
|
|
|
"ERR_QUEUE_NAME_ALREADY_EXISTS",
|
|
|
|
async value => {
|
|
|
|
if (value) {
|
|
|
|
const queueWithSameName = await Queue.findOne({
|
|
|
|
where: { name: value, id: { [Op.not]: queueId } }
|
|
|
|
});
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
return !queueWithSameName;
|
|
|
|
}
|
|
|
|
return true;
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-04-20 18:48:42 +00:00
|
|
|
),
|
|
|
|
color: Yup.string()
|
|
|
|
.required("ERR_QUEUE_INVALID_COLOR")
|
|
|
|
.test("Check-color", "ERR_QUEUE_INVALID_COLOR", async value => {
|
2022-01-06 01:26:15 +00:00
|
|
|
if (value) {
|
2023-04-20 18:48:42 +00:00
|
|
|
const colorTestRegex = /^#[0-9a-f]{3,6}$/i;
|
|
|
|
return colorTestRegex.test(value);
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
return true;
|
2023-04-20 18:48:42 +00:00
|
|
|
})
|
|
|
|
.test(
|
|
|
|
"Check-color-exists",
|
|
|
|
"ERR_QUEUE_COLOR_ALREADY_EXISTS",
|
|
|
|
async value => {
|
|
|
|
if (value) {
|
|
|
|
const queueWithSameColor = await Queue.findOne({
|
|
|
|
where: { color: value, id: { [Op.not]: queueId } }
|
|
|
|
});
|
|
|
|
return !queueWithSameColor;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
try {
|
|
|
|
await queueSchema.validate({ color, name });
|
|
|
|
} catch (err: any) {
|
|
|
|
throw new AppError(err.message);
|
|
|
|
}
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
const queue = await ShowQueueService(queueId);
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
await queue.update(queueData);
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2024-04-12 21:33:15 +00:00
|
|
|
const { greetingMessage, farewellMessage } = queue;
|
|
|
|
await set(`queue:${queueId}`, {
|
|
|
|
id: queueId,
|
|
|
|
name,
|
|
|
|
greetingMessage,
|
|
|
|
farewellMessage
|
|
|
|
});
|
2024-03-13 14:19:40 +00:00
|
|
|
|
2023-04-20 18:48:42 +00:00
|
|
|
return queue;
|
|
|
|
} catch (error: any) {
|
2024-03-13 14:19:40 +00:00
|
|
|
console.error("===> Error on UpdateQueueService.ts file: \n", error);
|
2023-04-20 18:48:42 +00:00
|
|
|
throw new AppError(error.message);
|
2024-03-13 14:19:40 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default UpdateQueueService;
|