fix: use amount from request
This commit is contained in:
parent
866d821d9d
commit
9b212aa529
@ -9,6 +9,7 @@ import { validateTariff } from "./helpers";
|
|||||||
|
|
||||||
import type { FastifyReply } from "fastify";
|
import type { FastifyReply } from "fastify";
|
||||||
import type { Privilege } from "@/types/models/privilege.type";
|
import type { Privilege } from "@/types/models/privilege.type";
|
||||||
|
import type { Tariff } from "@/types/models/tariff.type";
|
||||||
import type { Eloquent } from "@/types/models/eloquent.type";
|
import type { Eloquent } from "@/types/models/eloquent.type";
|
||||||
import type {
|
import type {
|
||||||
CreateTariffRequest,
|
CreateTariffRequest,
|
||||||
@ -55,34 +56,39 @@ export const getTariff = async (request: GetTariffRequest, reply: FastifyReply)
|
|||||||
return tariff;
|
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);
|
const [requestBody, error] = validateTariff(request.body);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const privilege of requestBody.privilegies) {
|
for (const privilege of requestBody.privilegies) {
|
||||||
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
||||||
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
||||||
|
const privilegies = await PrivilegeModel.find({ privilegeId: privilegeIDs }).lean();
|
||||||
|
|
||||||
const privilegies = await PrivilegeModel.find({ _id: privilegeIDs }).lean();
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
||||||
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => ({
|
const currentPrivilege = privilegiesMap.get(privilege.privilegeId);
|
||||||
|
|
||||||
|
return {
|
||||||
name: privilege.name,
|
name: privilege.name,
|
||||||
privilegeId: privilege.privilegeId,
|
privilegeId: privilege.privilegeId,
|
||||||
serviceKey: privilege.serviceKey,
|
serviceKey: privilege.serviceKey,
|
||||||
description: privilege.description,
|
description: privilege.description,
|
||||||
amount: privilege.amount,
|
amount: currentPrivilege?.amount ?? 0,
|
||||||
type: privilege.type,
|
type: privilege.type,
|
||||||
value: privilege.value,
|
value: privilege.value,
|
||||||
price: privilege.price,
|
price: privilege.price,
|
||||||
}));
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const newTariff = new TariffModel({
|
const newTariff = new TariffModel({
|
||||||
name: requestBody.name,
|
name: requestBody.name,
|
||||||
@ -123,19 +129,24 @@ export const replaceTariff = async (request: ReplaceTariffRequest, reply: Fastif
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
||||||
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
||||||
|
const privilegies = await PrivilegeModel.find({ privilegeId: privilegeIDs }).lean();
|
||||||
|
|
||||||
const privilegies = await PrivilegeModel.find({ _id: privilegeIDs }).lean();
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
||||||
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => ({
|
const currentPrivilege = privilegiesMap.get(privilege.privilegeId);
|
||||||
|
|
||||||
|
return {
|
||||||
name: privilege.name,
|
name: privilege.name,
|
||||||
privilegeId: privilege.privilegeId,
|
privilegeId: privilege.privilegeId,
|
||||||
serviceKey: privilege.serviceKey,
|
serviceKey: privilege.serviceKey,
|
||||||
description: privilege.description,
|
description: privilege.description,
|
||||||
amount: privilege.amount,
|
amount: currentPrivilege?.amount ?? 0,
|
||||||
type: privilege.type,
|
type: privilege.type,
|
||||||
value: privilege.value,
|
value: privilege.value,
|
||||||
price: privilege.price,
|
price: privilege.price,
|
||||||
}));
|
};
|
||||||
|
});
|
||||||
|
|
||||||
await tariff.replaceOne({
|
await tariff.replaceOne({
|
||||||
name: requestBody.name,
|
name: requestBody.name,
|
||||||
|
Loading…
Reference in New Issue
Block a user