2025-05-14 21:45:16 +02:00

87 lines
4.3 KiB
TypeScript

import { Resolver, Authorized, Query, Ctx, Arg, Args, Mutation } from 'type-graphql';
import { ApolloContext } from '@seed/interfaces/context';
import OrderModel from '@services/module-payments/functions/orders/order.model';
import { OrderEngineSchema } from '../../schemas/order.schema';
import { OrderArgs } from '../../schemas/order.schema.input';
import { getCurrentCartOrder } from '../../services/CartService';
import { newError } from '@seed/helpers/Error';
import { localCheckout } from '../../services/OrderService';
@Resolver(OrderModel)
export default class OrderEngineResolver {
/*
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
*/
@Query(() => OrderEngineSchema)
@Authorized()
async myCurrentOrder(@Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
const cartOrderData = await getCurrentCartOrder(ctx);
if (!cartOrderData) throw newError(6003);
return cartOrderData;
}
@Query(() => OrderEngineSchema)
@Authorized()
async myOrdersGetOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
const account = ctx.ctx.user;
return new OrderModel().getOne({ query: { _id: id, accountId: account._id } });
}
@Query(() => [OrderEngineSchema])
@Authorized()
async myOrdersGetMany(@Args() arg: OrderArgs, @Ctx() ctx: ApolloContext): Promise<OrderEngineSchema[]> {
const account = ctx.ctx.user;
const args = { ...arg, accountId: account._id };
return new OrderModel().getManyGenericWithArgs(args, ctx);
}
@Query(() => Number)
@Authorized()
async myOrdersGetManyCount(@Args() arg: OrderArgs, @Ctx() ctx: ApolloContext): Promise<number> {
const account = ctx.ctx.user;
const args = { ...arg, accountId: account._id };
return new OrderModel().getCountGeneric(args, ctx);
}
/*
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
*/
@Mutation(() => OrderEngineSchema)
@Authorized()
async removeCurrentOrder(@Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
try {
const account = ctx.ctx.user;
return new OrderModel().deleteOne({ query: { accountId: account._id }, ctx });
} catch (error) {
throw error;
}
}
@Mutation(() => OrderEngineSchema)
async validateOrder(@Arg('id') id: string): Promise<OrderEngineSchema> {
try {
const order = await new OrderModel().getOne({ query: { _id: id } });
await localCheckout(order);
return order;
} catch (error) {
throw error;
}
}
}