oneuptime/CommonServer/Services/BillingPaymentMethodService.ts

118 lines
3.7 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';
2022-11-17 20:06:52 +00:00
import DatabaseService, { OnDelete, OnFind } from './DatabaseService';
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';
import BillingService from './BillingService';
2022-11-17 20:06:52 +00:00
import DeleteBy from '../Types/Database/DeleteBy';
2022-11-17 22:02:37 +00:00
import LIMIT_MAX 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
const paymentMethods = 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
},
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) {
const billingPaymentMethod = new Model();
billingPaymentMethod.projectId = findBy.props.tenantId!;
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>> {
const items = await this.findBy({
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();