90 lines
4.5 KiB
TypeScript
90 lines
4.5 KiB
TypeScript
import { Resolver, Query, FieldResolver, Root, Arg, Mutation, Ctx, Args, Authorized, ResolverInterface } from 'type-graphql';
|
|
|
|
import VatClassModel from './vat.model';
|
|
import OrderEngineModel from '@services/module-payments/functions/orders/order.model';
|
|
import { ApolloContext } from '@seed/interfaces/context';
|
|
import { ValidateVatInput, VatResponse } from './vat.model';
|
|
import axios from 'axios';
|
|
import { updatePaymentIntent } from '@services/module-payments/helpers/payments.helpers';
|
|
import { PaymentIntentInfo } from '@services/module-payments/components/components.payments';
|
|
import { getCurrentCartOrder } from '../orders/services/CartService';
|
|
|
|
@Resolver(VatClassModel)
|
|
export default class VatClassResolver {
|
|
/*
|
|
██████╗ ██╗ ██╗███████╗██████╗ ██╗ ██╗
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗╚██╗ ██╔╝
|
|
██║ ██║██║ ██║█████╗ ██████╔╝ ╚████╔╝
|
|
██║▄▄ ██║██║ ██║██╔══╝ ██╔══██╗ ╚██╔╝
|
|
╚██████╔╝╚██████╔╝███████╗██║ ██║ ██║
|
|
╚══▀▀═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝
|
|
*/
|
|
/*
|
|
███╗ ███╗██╗ ██╗████████╗ █████╗ ████████╗ ██████╗ ██████╗ ███████╗
|
|
████╗ ████║██║ ██║╚══██╔══╝██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝
|
|
██╔████╔██║██║ ██║ ██║ ███████║ ██║ ██║ ██║██████╔╝███████╗
|
|
██║╚██╔╝██║██║ ██║ ██║ ██╔══██║ ██║ ██║ ██║██╔══██╗╚════██║
|
|
██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ╚██████╔╝██║ ██║███████║
|
|
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
|
|
*/
|
|
@Mutation(() => Boolean)
|
|
@Authorized()
|
|
async vatValidate(@Arg('input') input: ValidateVatInput, @Ctx() ctx: ApolloContext): Promise<boolean> {
|
|
//VATLAYER
|
|
const baseUrl = 'http://apilayer.net/api/validate?';
|
|
const account = ctx.ctx.user;
|
|
const orderModel = new OrderEngineModel();
|
|
|
|
try {
|
|
const currentCartOrder = await getCurrentCartOrder(ctx);
|
|
|
|
let valid = false;
|
|
let vatExempt = false;
|
|
|
|
if (input.countryCode === process.env.SHOP_COUNTRY_CODE) valid = true;
|
|
else {
|
|
const vatResponse = await axios.get(
|
|
`${baseUrl}access_key=${process.env.VATLAYER_API_KEY}&vat_number=${input.countryCode}${input.vatNumber}`,
|
|
);
|
|
if (vatResponse.data.valid) {
|
|
vatExempt = true;
|
|
valid = true;
|
|
}
|
|
}
|
|
|
|
//Pas top de faire 2 requetes
|
|
|
|
await orderModel.updateOneCustom({
|
|
query: { _id: currentCartOrder._id },
|
|
updateRequest: {
|
|
$set: {
|
|
vatExempt: vatExempt,
|
|
},
|
|
},
|
|
ctx,
|
|
});
|
|
|
|
//ne marche pas on dirait
|
|
const paymentIntentUpdated: PaymentIntentInfo = await updatePaymentIntent(currentCartOrder.paymentIntent, {
|
|
amount: await currentCartOrder.finalPrice(ctx),
|
|
currency: currentCartOrder.currency,
|
|
account,
|
|
});
|
|
|
|
await orderModel.updateOneCustom({
|
|
query: { _id: currentCartOrder._id },
|
|
updateRequest: {
|
|
$set: {
|
|
paymentIntent: paymentIntentUpdated,
|
|
},
|
|
},
|
|
ctx,
|
|
});
|
|
|
|
return valid;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|