198 lines
5.3 KiB
TypeScript
198 lines
5.3 KiB
TypeScript
import { Types } from "mongoose";
|
|
|
|
import { TariffModel } from "@/models/tariff.model";
|
|
import { PrivilegeModel } from "@/models/privilege.model";
|
|
|
|
import { validateEmptyFields } from "@/utils/validate-empty-fields";
|
|
import { validateTariff } from "./helpers";
|
|
|
|
import type { FastifyReply } from "fastify";
|
|
|
|
import type { Privilege } from "@/types/models/privilege.type";
|
|
import type { Eloquent } from "@/types/models/eloquent.type";
|
|
import type { CreateTariffRequest, GetTariffRequest, ReplaceTariffRequest, RemoveTariffRequest } from "./types";
|
|
|
|
export const getTariffs = async () => TariffModel.find({}).lean();
|
|
|
|
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
|
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
|
|
|
if (error) {
|
|
reply.status(400);
|
|
return error;
|
|
}
|
|
|
|
if (!Types.ObjectId.isValid(requestParams.id)) {
|
|
reply.status(400);
|
|
return new Error("invalid id");
|
|
}
|
|
|
|
const tariff = await TariffModel.findById(requestParams.id).lean();
|
|
|
|
if (!tariff) {
|
|
reply.status(404);
|
|
return new Error("tariff not found");
|
|
}
|
|
|
|
return tariff;
|
|
};
|
|
|
|
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply) => {
|
|
const [requestBody, error] = validateTariff(request.body);
|
|
|
|
if (error) {
|
|
reply.status(400);
|
|
return error;
|
|
}
|
|
|
|
for (const privilegeId of requestBody.privilegieIDArray) {
|
|
if (!Types.ObjectId.isValid(privilegeId)) {
|
|
reply.status(404);
|
|
return new Error(`privilege id <${privilegeId}> invalid`);
|
|
}
|
|
}
|
|
|
|
const privilegies = await PrivilegeModel.find({ privilegeId: requestBody.privilegieIDArray }).lean();
|
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => ({
|
|
name: privilege.name,
|
|
privilegeId: privilege.name,
|
|
serviceKey: privilege.serviceKey,
|
|
description: privilege.description,
|
|
amount: privilege.amount,
|
|
type: privilege.type,
|
|
value: privilege.value,
|
|
price: privilege.price,
|
|
}));
|
|
|
|
const newTariff = new TariffModel({
|
|
name: requestBody.name,
|
|
price: requestBody.price,
|
|
isCustom: requestBody.isCustom,
|
|
privilegies: cleanPrivilegies,
|
|
});
|
|
|
|
await newTariff.save();
|
|
|
|
return newTariff;
|
|
};
|
|
|
|
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
|
const [requestBody, error] = validateTariff(request.body ?? {});
|
|
|
|
if (error) {
|
|
reply.status(400);
|
|
return error;
|
|
}
|
|
|
|
if (!Types.ObjectId.isValid(request.params?.id || "")) {
|
|
reply.status(400);
|
|
return new Error("invalid id");
|
|
}
|
|
|
|
const tariff = await TariffModel.findById(request.params?.id);
|
|
|
|
if (!tariff) {
|
|
reply.status(404);
|
|
return new Error("tariff not found");
|
|
}
|
|
|
|
for (const privilegeId of requestBody.privilegieIDArray) {
|
|
if (!Types.ObjectId.isValid(privilegeId)) {
|
|
reply.status(404);
|
|
return new Error(`privilege id <${privilegeId}> invalid`);
|
|
}
|
|
}
|
|
|
|
const privilegies = await PrivilegeModel.find({ privilegeId: requestBody.privilegieIDArray }).lean();
|
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => ({
|
|
name: privilege.name,
|
|
privilegeId: privilege.name,
|
|
serviceKey: privilege.serviceKey,
|
|
description: privilege.description,
|
|
amount: privilege.amount,
|
|
type: privilege.type,
|
|
value: privilege.value,
|
|
price: privilege.price,
|
|
}));
|
|
|
|
await tariff.replaceOne({
|
|
name: requestBody.name,
|
|
price: requestBody.price,
|
|
isCustom: requestBody.isCustom,
|
|
privilegies: cleanPrivilegies,
|
|
});
|
|
|
|
return tariff;
|
|
};
|
|
|
|
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
if (error) {
|
|
reply.status(400);
|
|
return error;
|
|
}
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
reply.status(400);
|
|
return new Error("invalid id");
|
|
}
|
|
|
|
const tariff = await TariffModel.findByIdAndUpdate(id, {
|
|
$set: { isDeleted: true, deletedAt: new Date() },
|
|
}).lean();
|
|
|
|
if (!tariff) {
|
|
reply.status(404);
|
|
return new Error("tariff not found");
|
|
}
|
|
|
|
return tariff;
|
|
};
|
|
|
|
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
if (error) {
|
|
reply.status(400);
|
|
return error;
|
|
}
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
reply.status(400);
|
|
return new Error("invalid id");
|
|
}
|
|
|
|
const tariff = await TariffModel.findByIdAndDelete(id).lean();
|
|
|
|
if (!tariff) {
|
|
reply.status(404);
|
|
return new Error("tariff not found");
|
|
}
|
|
|
|
return tariff;
|
|
};
|
|
|
|
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
if (error) {
|
|
reply.status(400);
|
|
return error;
|
|
}
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
reply.status(400);
|
|
return new Error("invalid id");
|
|
}
|
|
|
|
const tariff = await TariffModel.findByIdAndUpdate(id, { $set: { isDeleted: false } }).lean();
|
|
|
|
if (!tariff) {
|
|
reply.status(404);
|
|
return new Error("tariff not found");
|
|
}
|
|
|
|
return tariff;
|
|
};
|