117 lines
6.5 KiB
TypeScript
117 lines
6.5 KiB
TypeScript
import { Arg, Args, Ctx, Mutation, Resolver, Authorized } from 'type-graphql';
|
|
import { ApolloContext } from '@lib/seed/interfaces/context';
|
|
import OrderEngineModel from '@services/module-payments/functions/orders/order.model';
|
|
import { OrderEngineSchema } from '../../schemas/order.schema';
|
|
import { createBasePublicResolver } from '@seed/graphql/baseResolvers/BasePublicResolver';
|
|
import { AccountTypeEnum } from '@src/accounts/account.components';
|
|
import { ApolloError } from 'apollo-server-lambda';
|
|
|
|
import StripeService from '@services/module-payments/services/stripe/StripeService';
|
|
import { OrderStatusEnum } from '@services/module-payments/components/components.orders';
|
|
import { PaymentStatusEnum } from '@services/module-payments/components/components.payments';
|
|
import { AsyncHooksService } from '@lib/__hooks';
|
|
|
|
@Resolver(OrderEngineModel)
|
|
export default class OrderAdminEngineMutationResolver {
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
/*
|
|
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
|
|
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
|
|
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
|
|
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
|
|
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
|
|
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
|
|
*/
|
|
@Mutation(() => OrderEngineSchema)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async ordersMarkOneAsPaid(@Arg('id') orderId: string, @Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
|
|
const model = new OrderEngineModel();
|
|
const currentOrder = await model.getOne({ query: { _id: orderId }, ctx });
|
|
|
|
if (currentOrder.paymentStatus == PaymentStatusEnum.paid) throw new ApolloError('orders.payment.alreadypaid', '400');
|
|
if (currentOrder.paymentStatus == PaymentStatusEnum.free) throw new ApolloError('orders.payment.freeItem', '400');
|
|
|
|
const result = await model.updateOne({
|
|
query: { _id: currentOrder._id },
|
|
newData: {
|
|
paymentStatus: PaymentStatusEnum.paid,
|
|
},
|
|
ctx,
|
|
});
|
|
await new AsyncHooksService().afterOrdersMarkOneAsPaid(result, ctx);
|
|
return result;
|
|
}
|
|
|
|
@Mutation(() => OrderEngineSchema)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async ordersMarkOneAsUnPaid(@Arg('id') orderId: string, @Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
|
|
const model = new OrderEngineModel();
|
|
const currentOrder = await model.getOne({ query: { _id: orderId }, ctx });
|
|
|
|
if (currentOrder.paymentStatus == PaymentStatusEnum.pending) throw new ApolloError('orders.payment.alreadyUnPaid', '400');
|
|
if (currentOrder.paymentStatus == PaymentStatusEnum.free) throw new ApolloError('orders.payment.freeItem', '400');
|
|
|
|
const result = await model.updateOne({
|
|
query: { _id: currentOrder._id },
|
|
newData: {
|
|
paymentStatus: PaymentStatusEnum.pending,
|
|
},
|
|
ctx,
|
|
});
|
|
await new AsyncHooksService().afterOrdersMarkOneAsUnPaid(result, ctx);
|
|
return result;
|
|
}
|
|
|
|
@Mutation(() => OrderEngineSchema)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async ordersCancelOne(@Arg('id') orderId: string, @Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
|
|
const model = new OrderEngineModel();
|
|
const currentOrder = await model.getOne({ query: { _id: orderId }, ctx });
|
|
|
|
if (currentOrder.orderStatus == OrderStatusEnum.canceled) throw new ApolloError('orders.alreadyCancel', '400');
|
|
if (currentOrder.orderStatus == OrderStatusEnum.refund) throw new ApolloError('orders.alreadyRefund', '400');
|
|
|
|
const result = await model.updateOne({
|
|
query: { _id: currentOrder._id },
|
|
newData: {
|
|
orderStatus: OrderStatusEnum.canceled,
|
|
},
|
|
ctx,
|
|
});
|
|
await new AsyncHooksService().afterOrderCancel(result, ctx);
|
|
return result;
|
|
}
|
|
|
|
@Mutation(() => OrderEngineSchema)
|
|
@Authorized(AccountTypeEnum.admin)
|
|
async ordersReimburseOne(@Arg('orderId') orderId: string, @Ctx() ctx: ApolloContext): Promise<OrderEngineSchema> {
|
|
const model = new OrderEngineModel();
|
|
const currentOrder = await model.getOne({ query: { _id: orderId }, ctx });
|
|
if (currentOrder.orderStatus != OrderStatusEnum.canceled) throw new ApolloError('orders.reimburse.notCanceled', '400');
|
|
try {
|
|
if (currentOrder.paymentInfo?.transactionId) {
|
|
await StripeService.getInstance().reimburse(currentOrder.paymentInfo.transactionId);
|
|
const result = await model.updateOne({
|
|
query: { _id: currentOrder._id },
|
|
newData: {
|
|
orderStatus: OrderStatusEnum.refund,
|
|
},
|
|
ctx,
|
|
});
|
|
await new AsyncHooksService().afterOrdersReimbursed(result, ctx);
|
|
return result;
|
|
}
|
|
throw new ApolloError('orders.reimburse.noTransactionFound', '400');
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|