|
|
|
@ -23,114 +23,100 @@ import type {
|
|
|
|
|
export const getTariffs = async (request: GetTariffsRequest): Promise<GetTariffsResponse> => {
|
|
|
|
|
const { page, limit } = determinePaginationParameters(request?.query ?? {});
|
|
|
|
|
|
|
|
|
|
const tariffsCount = await TariffModel.countDocuments();
|
|
|
|
|
const tariffsCount = await TariffModel.countDocuments({
|
|
|
|
|
$or: [{ isCustom: false }, { isCustom: true, userId: request.user.id }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const totalPages = Math.ceil(tariffsCount / limit);
|
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
|
|
|
|
|
|
const tariffs = await TariffModel.find({}).sort({ createdAt: "desc" }).skip(offset).limit(limit).lean();
|
|
|
|
|
const tariffs = await TariffModel.find({ $or: [{ isCustom: false }, { isCustom: true, userId: request.user.id }] })
|
|
|
|
|
.sort({ createdAt: "desc" })
|
|
|
|
|
.skip(offset)
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
return { tariffs, totalPages };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
|
|
|
|
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
|
|
|
|
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return error;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(requestParams.id)) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return new Error("invalid id");
|
|
|
|
|
throw new Error("invalid id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findById(requestParams.id).lean();
|
|
|
|
|
const tariff = await TariffModel.findOne({
|
|
|
|
|
$or: [
|
|
|
|
|
{ _id: new Types.ObjectId(requestParams.id), isCustom: false },
|
|
|
|
|
{ _id: new Types.ObjectId(requestParams.id), isCustom: true, userId: request.user.id },
|
|
|
|
|
],
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
return new Error("tariff not found");
|
|
|
|
|
throw new Error("tariff not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply) => {
|
|
|
|
|
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
|
|
|
|
const [requestBody, error] = validateTariff(request.body);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
console.info("this 1");
|
|
|
|
|
return error;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const privilege of requestBody.privilegies) {
|
|
|
|
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
console.info("this 2");
|
|
|
|
|
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
|
|
|
|
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.info("this 3");
|
|
|
|
|
|
|
|
|
|
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
|
|
|
|
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
|
|
|
|
PrivilegeModel.find({ _id: privilegeIDs }).lean().then(privilegies => {
|
|
|
|
|
console.info("this 4", privilegies);
|
|
|
|
|
|
|
|
|
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
|
|
|
|
const currentPrivilege = privilegiesMap.get(privilege._id.toString());
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
name: privilege.name,
|
|
|
|
|
privilegeId: privilege.privilegeId,
|
|
|
|
|
serviceKey: privilege.serviceKey,
|
|
|
|
|
description: privilege.description,
|
|
|
|
|
amount: currentPrivilege?.amount ?? 0,
|
|
|
|
|
type: privilege.type,
|
|
|
|
|
value: privilege.value,
|
|
|
|
|
price: privilege.price,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const newTariff = new TariffModel({
|
|
|
|
|
name: requestBody.name,
|
|
|
|
|
price: requestBody.price,
|
|
|
|
|
isCustom: requestBody.isCustom,
|
|
|
|
|
privilegies: requestBody.privilegies,
|
|
|
|
|
});
|
|
|
|
|
console.info("this 5");
|
|
|
|
|
|
|
|
|
|
newTariff.save();
|
|
|
|
|
const newTariff = new TariffModel({
|
|
|
|
|
name: requestBody.name,
|
|
|
|
|
price: requestBody.price,
|
|
|
|
|
userId: request.user.id,
|
|
|
|
|
isCustom: requestBody.isCustom,
|
|
|
|
|
privilegies: requestBody.privilegies,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return newTariff.save();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
|
|
|
|
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
|
|
|
|
const [requestBody, error] = validateTariff(request.body ?? {});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return error;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(request.params?.id || "")) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return new Error("invalid id");
|
|
|
|
|
throw new Error("invalid id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findById(request.params?.id);
|
|
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
return new Error("tariff not found");
|
|
|
|
|
throw new Error("tariff not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const privilege of requestBody.privilegies) {
|
|
|
|
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
|
|
|
|
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -163,17 +149,17 @@ export const replaceTariff = async (request: ReplaceTariffRequest, reply: Fastif
|
|
|
|
|
return tariff;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
|
|
|
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
|
|
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return error;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return new Error("invalid id");
|
|
|
|
|
throw new Error("invalid id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findByIdAndUpdate(id, {
|
|
|
|
@ -182,53 +168,53 @@ export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyR
|
|
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
return new Error("tariff not found");
|
|
|
|
|
throw new Error("tariff not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
|
|
|
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
|
|
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return error;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return new Error("invalid id");
|
|
|
|
|
throw new Error("invalid id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findByIdAndDelete(id).lean();
|
|
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
return new Error("tariff not found");
|
|
|
|
|
throw new Error("tariff not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
|
|
|
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
|
|
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return error;
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
|
|
|
void reply.status(400);
|
|
|
|
|
return new Error("invalid id");
|
|
|
|
|
throw new Error("invalid id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findByIdAndUpdate(id, { $set: { isDeleted: false } }).lean();
|
|
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
|
void reply.status(404);
|
|
|
|
|
return new Error("tariff not found");
|
|
|
|
|
throw new Error("tariff not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
|