2022-12-21 20:06:04 +00:00
|
|
|
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 { CreateTariffRequest, GetTariffRequest, ReplaceTariffRequest } 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 privilege of requestBody.privilegies) {
|
|
|
|
const findedPrivilege = await PrivilegeModel.findOne({ privilegeId: privilege.privilegeId }).lean();
|
|
|
|
|
|
|
|
if (!findedPrivilege) {
|
|
|
|
reply.status(404);
|
|
|
|
return new Error(`privilege with id ${privilege.privilegeId} not found`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 10:45:44 +00:00
|
|
|
const privilegiesMap = requestBody.privilegies.reduce<Record<string, Privilege>>((accamulator, privilege) => {
|
|
|
|
accamulator[privilege.privilegeId] = privilege;
|
2022-12-21 20:06:04 +00:00
|
|
|
|
|
|
|
return accamulator;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const newTariff = new TariffModel({
|
|
|
|
name: requestBody.name,
|
|
|
|
price: requestBody.price,
|
|
|
|
isCustom: requestBody.isCustom,
|
|
|
|
privilegies: privilegiesMap,
|
|
|
|
});
|
|
|
|
|
|
|
|
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 privilege of requestBody.privilegies) {
|
|
|
|
const findedPrivilege = await PrivilegeModel.findOne({ privilegeId: privilege.privilegeId }).lean();
|
|
|
|
|
|
|
|
if (!findedPrivilege) {
|
|
|
|
reply.status(404);
|
|
|
|
return new Error(`privilege with id ${privilege.privilegeId} not found`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const privilegiesMap = requestBody.privilegies.reduce<Record<string, Privilege[]>>((accamulator, privilege) => {
|
|
|
|
if (!accamulator[privilege.privilegeId]) {
|
|
|
|
accamulator[privilege.privilegeId] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
accamulator[privilege.privilegeId].push(privilege);
|
|
|
|
|
|
|
|
return accamulator;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
await tariff.replaceOne({
|
|
|
|
name: requestBody.name,
|
|
|
|
price: requestBody.price,
|
|
|
|
isCustom: requestBody.isCustom,
|
|
|
|
privilegies: privilegiesMap,
|
|
|
|
});
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
};
|