feat: finished queues crud services

This commit is contained in:
canove
2021-01-10 10:59:01 -03:00
parent cca253cd0a
commit d374c84b60
8 changed files with 216 additions and 11 deletions

View File

@@ -0,0 +1,67 @@
import * as Yup from "yup";
import AppError from "../../errors/AppError";
import Queue from "../../models/Queue";
interface QueueData {
name: string;
color: string;
greetingMessage?: string;
}
const CreateQueueService = async (queueData: QueueData): Promise<Queue> => {
const { color, name } = queueData;
const queueSchema = Yup.object().shape({
name: Yup.string()
.min(2, "ERR_QUEUE_INVALID_NAME")
.required("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 }
});
return !queueWithSameName;
}
return false;
}
),
color: Yup.string()
.required("ERR_QUEUE_INVALID_COLOR")
.test("Check-color", "ERR_QUEUE_INVALID_COLOR", async value => {
if (value) {
const colorTestRegex = /^#[0-9a-f]{3,6}$/i;
return colorTestRegex.test(value);
}
return false;
})
.test(
"Check-color-exists",
"ERR_QUEUE_COLOR_ALREADY_EXISTS",
async value => {
if (value) {
const queueWithSameColor = await Queue.findOne({
where: { color: value }
});
return !queueWithSameColor;
}
return false;
}
)
});
try {
await queueSchema.validate({ color, name });
} catch (err) {
throw new AppError(err.message);
}
const queue = await Queue.create(queueData);
return queue;
};
export default CreateQueueService;

View File

@@ -0,0 +1,9 @@
import ShowQueueService from "./ShowQueueService";
const DeleteQueueService = async (queueId: number | string): Promise<void> => {
const queue = await ShowQueueService(queueId);
await queue.destroy();
};
export default DeleteQueueService;

View File

@@ -0,0 +1,9 @@
import Queue from "../../models/Queue";
const ListQueuesService = async (): Promise<Queue[]> => {
const queues = await Queue.findAll();
return queues;
};
export default ListQueuesService;

View File

@@ -0,0 +1,14 @@
import AppError from "../../errors/AppError";
import Queue from "../../models/Queue";
const ShowQueueService = async (queueId: number | string): Promise<Queue> => {
const queue = await Queue.findByPk(queueId);
if (!queue) {
throw new AppError("ERR_QUEUE_NOT_FOUND");
}
return queue;
};
export default ShowQueueService;

View File

@@ -0,0 +1,73 @@
import { Op } from "sequelize";
import * as Yup from "yup";
import AppError from "../../errors/AppError";
import Queue from "../../models/Queue";
import ShowQueueService from "./ShowQueueService";
interface QueueData {
name?: string;
color?: string;
greetingMessage?: string;
}
const UpdateQueueService = async (
queueId: number | string,
queueData: QueueData
): Promise<Queue> => {
const { color, name } = queueData;
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 } }
});
return !queueWithSameName;
}
return true;
}
),
color: Yup.string()
.required("ERR_QUEUE_INVALID_COLOR")
.test("Check-color", "ERR_QUEUE_INVALID_COLOR", async value => {
if (value) {
const colorTestRegex = /^#[0-9a-f]{3,6}$/i;
return colorTestRegex.test(value);
}
return true;
})
.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;
}
)
});
try {
await queueSchema.validate({ color, name });
} catch (err) {
throw new AppError(err.message);
}
const queue = await ShowQueueService(queueId);
await queue.update(queueData);
return queue;
};
export default UpdateQueueService;