oneuptime/CommonServer/Services/BillingPaymentMethodService.ts

124 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-17 15:11:23 +00:00
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
import Model from 'Model/Models/BillingPaymentMethod';
2023-10-02 09:32:31 +00:00
import DatabaseService from './DatabaseService';
import { OnDelete, OnFind } from '../Types/Database/Hooks';
2022-11-17 19:41:53 +00:00
import FindBy from '../Types/Database/FindBy';
import ProjectService from './ProjectService';
import BadDataException from 'Common/Types/Exception/BadDataException';
import Project from 'Model/Models/Project';
2022-11-25 14:57:58 +00:00
import BillingService, { PaymentMethod } from './BillingService';
2022-11-17 20:06:52 +00:00
import DeleteBy from '../Types/Database/DeleteBy';
2023-02-22 15:05:14 +00:00
import LIMIT_MAX, { LIMIT_PER_PROJECT } from 'Common/Types/Database/LimitMax';
2022-11-17 15:11:23 +00:00
export class Service extends DatabaseService<Model> {
public constructor(postgresDatabase?: PostgresDatabase) {
super(Model, postgresDatabase);
}
2022-11-17 19:41:53 +00:00
2022-11-17 22:13:13 +00:00
protected override async onBeforeFind(
findBy: FindBy<Model>
): Promise<OnFind<Model>> {
2022-11-17 19:41:53 +00:00
if (!findBy.props.tenantId) {
2022-11-17 22:13:13 +00:00
throw new BadDataException('ProjectID not found.');
2022-11-17 19:41:53 +00:00
}
2022-11-17 22:13:13 +00:00
2022-11-17 19:41:53 +00:00
const project: Project | null = await ProjectService.findOneById({
id: findBy.props.tenantId!,
props: {
2022-11-17 22:13:13 +00:00
...findBy.props,
2022-11-17 22:02:37 +00:00
isRoot: true,
2022-11-17 22:13:13 +00:00
ignoreHooks: true,
2022-11-17 19:41:53 +00:00
},
select: {
_id: true,
2022-11-17 22:13:13 +00:00
paymentProviderCustomerId: true,
},
2022-11-17 19:41:53 +00:00
});
if (!project) {
2022-11-17 22:13:13 +00:00
throw new BadDataException('Project not found');
2022-11-17 19:41:53 +00:00
}
if (!project.paymentProviderCustomerId) {
2022-11-17 22:13:13 +00:00
throw new BadDataException(
'Payment provider customer id not found.'
);
2022-11-17 19:41:53 +00:00
}
2022-11-17 22:13:13 +00:00
2022-11-25 14:57:58 +00:00
const paymentMethods: Array<PaymentMethod> =
await BillingService.getPaymentMethods(
project.paymentProviderCustomerId
);
2022-11-17 19:41:53 +00:00
await this.deleteBy({
query: {
2022-11-17 22:13:13 +00:00
projectId: findBy.props.tenantId!,
2022-11-17 19:41:53 +00:00
},
2023-02-24 21:42:00 +00:00
limit: LIMIT_PER_PROJECT,
2023-02-22 15:05:14 +00:00
skip: 0,
2022-11-17 19:41:53 +00:00
props: {
2022-11-17 20:06:52 +00:00
isRoot: true,
2022-11-17 22:13:13 +00:00
ignoreHooks: true,
},
2022-11-17 19:41:53 +00:00
});
for (const paymentMethod of paymentMethods) {
2022-11-25 14:57:58 +00:00
const billingPaymentMethod: Model = new Model();
2022-11-22 13:13:11 +00:00
2022-11-22 20:18:12 +00:00
billingPaymentMethod.projectId = project.id!;
2022-11-22 13:13:11 +00:00
2022-11-17 19:41:53 +00:00
billingPaymentMethod.type = paymentMethod.type;
billingPaymentMethod.last4Digits = paymentMethod.last4Digits;
billingPaymentMethod.isDefault = paymentMethod.isDefault;
2022-11-17 22:13:13 +00:00
billingPaymentMethod.paymentProviderPaymentMethodId =
paymentMethod.id;
billingPaymentMethod.paymentProviderCustomerId =
project.paymentProviderCustomerId;
2022-11-17 22:02:37 +00:00
2022-11-17 19:41:53 +00:00
await this.create({
data: billingPaymentMethod,
props: {
2022-11-17 22:13:13 +00:00
isRoot: true,
},
2022-11-17 19:41:53 +00:00
});
}
return { findBy, carryForward: paymentMethods };
}
2022-11-17 22:13:13 +00:00
protected override async onBeforeDelete(
deleteBy: DeleteBy<Model>
): Promise<OnDelete<Model>> {
2022-11-25 14:57:58 +00:00
const items: Array<Model> = await this.findBy({
2022-11-17 22:13:13 +00:00
query: deleteBy.query,
select: {
_id: true,
paymentProviderPaymentMethodId: true,
paymentProviderCustomerId: true,
},
skip: 0,
limit: LIMIT_MAX,
props: {
isRoot: true,
ignoreHooks: true,
},
});
2022-11-17 22:02:37 +00:00
2022-11-17 22:13:13 +00:00
for (const item of items) {
if (
item.paymentProviderPaymentMethodId &&
item.paymentProviderCustomerId
) {
await BillingService.deletePaymentMethod(
item.paymentProviderCustomerId,
item.paymentProviderPaymentMethodId
);
}
}
2022-11-17 20:06:52 +00:00
2022-11-17 22:13:13 +00:00
return { deleteBy, carryForward: null };
}
2022-11-17 15:11:23 +00:00
}
export default new Service();