96 lines
4.9 KiB
TypeScript
96 lines
4.9 KiB
TypeScript
import { Resolver, Arg, Mutation, Ctx, Authorized } from 'type-graphql';
|
|
import AccountModel from '@src/accounts/account.model';
|
|
import { ApolloContext } from '@lib/seed/interfaces/context';
|
|
import StripeService from '@services/module-payments/services/stripe/StripeService';
|
|
import { ApolloError } from 'apollo-server-lambda';
|
|
import { BillingAddressComponent } from '@services/module-payments/components/components';
|
|
import { PaymentMethodDetail, PaymentMethodInput } from '@services/module-payments/components/components.payments';
|
|
import { newError } from '@seed/helpers/Error';
|
|
|
|
@Resolver(AccountModel)
|
|
export default class AccountPaymentMethodResolver {
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
/*
|
|
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
|
|
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
|
|
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
|
|
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
|
|
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
|
|
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
|
|
*/
|
|
|
|
@Mutation(() => PaymentMethodDetail)
|
|
@Authorized()
|
|
async accountsPaymentMethodsAddOne(@Arg('input') input: PaymentMethodInput, @Ctx() ctx: ApolloContext): Promise<BillingAddressComponent> {
|
|
const model = ctx.ctx.user;
|
|
|
|
// Get the card information from stripe
|
|
const pmResult = await StripeService.getInstance().createPaymentMethod(model, input.sPayMethodId);
|
|
|
|
const cardDetail = {
|
|
...input,
|
|
sPayMethodId: pmResult.id,
|
|
type: pmResult.type,
|
|
cardInfo: pmResult.card,
|
|
};
|
|
|
|
if (input.default && model.paymentInfo && model.paymentInfo.paymentMethods) {
|
|
// Remove all default from others
|
|
await model.updateOneCustom(
|
|
{ _id: model._id },
|
|
{
|
|
$set: { 'paymentInfo.paymentMethods.$[].default': false },
|
|
},
|
|
ctx,
|
|
);
|
|
} else input.default = false;
|
|
|
|
return await model.addOneSubRessource({ _id: model._id }, 'paymentInfo.paymentMethods', cardDetail, ctx);
|
|
}
|
|
|
|
@Mutation(() => PaymentMethodDetail)
|
|
@Authorized()
|
|
async accountsPaymentMethodsMarkasDefault(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<BillingAddressComponent> {
|
|
const model = ctx.ctx.user;
|
|
|
|
// Remove all default from others
|
|
await model.updateOneCustom(
|
|
{ _id: model._id, 'paymentInfo.paymentMethods._id': id },
|
|
{
|
|
$set: { 'paymentInfo.paymentMethods.$[].default': false },
|
|
},
|
|
ctx,
|
|
);
|
|
|
|
await model.updateOneCustom(
|
|
{ _id: model._id, 'paymentInfo.paymentMethods._id': id },
|
|
{
|
|
$set: { 'paymentInfo.paymentMethods.$.default': true },
|
|
},
|
|
ctx,
|
|
);
|
|
|
|
return await model.getOneSubRessource('paymentInfo.paymentMethods', id);
|
|
}
|
|
|
|
@Mutation(() => String)
|
|
@Authorized()
|
|
async accountsPaymentMethodsDeleteOne(@Arg('id') id: string, @Ctx() ctx: ApolloContext): Promise<string> {
|
|
const model = ctx.ctx.user;
|
|
|
|
// Get the PM id
|
|
const currentPM = model.getOneSubRessource('paymentInfo.paymentMethods', id) as PaymentMethodDetail;
|
|
if (!currentPM) throw newError(404);
|
|
|
|
// Get the card information from stripe
|
|
return await model.deleteOneSubRessource({ _id: model._id }, 'paymentInfo.paymentMethods', id, ctx);
|
|
}
|
|
}
|