2022-11-24 15:10:21 +00:00
|
|
|
import BaseModel from 'Common/Models/BaseModel';
|
|
|
|
import BadDataException from 'Common/Types/Exception/BadDataException';
|
|
|
|
import { JSONObject } from 'Common/Types/JSON';
|
|
|
|
import Permission from 'Common/Types/Permission';
|
|
|
|
import BillingInvoice from 'Model/Models/BillingInvoice';
|
|
|
|
import Project from 'Model/Models/Project';
|
|
|
|
import { IsBillingEnabled } from '../Config';
|
|
|
|
import UserMiddleware from '../Middleware/UserAuthorization';
|
|
|
|
import BillingInvoiceService, {
|
|
|
|
Service as BillingInvoiceServiceType,
|
|
|
|
} from '../Services/BillingInvoiceService';
|
|
|
|
import BillingService, { Invoice } from '../Services/BillingService';
|
|
|
|
import ProjectService from '../Services/ProjectService';
|
|
|
|
import {
|
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
NextFunction,
|
|
|
|
} from '../Utils/Express';
|
|
|
|
import Response from '../Utils/Response';
|
|
|
|
import BaseAPI from './BaseAPI';
|
|
|
|
|
|
|
|
export default class UserAPI extends BaseAPI<
|
|
|
|
BillingInvoice,
|
|
|
|
BillingInvoiceServiceType
|
|
|
|
> {
|
|
|
|
public constructor() {
|
|
|
|
super(BillingInvoice, BillingInvoiceService);
|
|
|
|
|
|
|
|
this.router.post(
|
|
|
|
`/${new this.entityType().getCrudApiPath()?.toString()}/pay`,
|
|
|
|
UserMiddleware.getUserMiddleware,
|
|
|
|
async (
|
|
|
|
req: ExpressRequest,
|
|
|
|
res: ExpressResponse,
|
|
|
|
next: NextFunction
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
if (!IsBillingEnabled) {
|
|
|
|
throw new BadDataException(
|
|
|
|
'Billign is not enabled for this server'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body['projectId']) {
|
|
|
|
throw new BadDataException(
|
|
|
|
'projectId is required in request body'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-24 15:13:21 +00:00
|
|
|
const userPermissions = (
|
|
|
|
await this.getPermissionsForTenant(req)
|
|
|
|
).filter((permission) => {
|
2022-11-24 15:10:21 +00:00
|
|
|
console.log(permission.permission);
|
|
|
|
//FIX: Change "Project"
|
|
|
|
return (
|
2022-11-24 15:13:21 +00:00
|
|
|
permission.permission.toString() ===
|
|
|
|
Permission.ProjectOwner.toString() ||
|
|
|
|
permission.permission.toString() ===
|
|
|
|
Permission.CanEditInvoices.toString()
|
2022-11-24 15:10:21 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (userPermissions.length === 0) {
|
|
|
|
throw new BadDataException(
|
|
|
|
`You need ${Permission.ProjectOwner} or ${Permission.CanEditInvoices} permission to pay invoices.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const project: Project | null =
|
|
|
|
await ProjectService.findOneById({
|
|
|
|
id: this.getTenantId(req)!,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
paymentProviderCustomerId: true,
|
2022-11-24 15:13:21 +00:00
|
|
|
paymentProviderSubscriptionId: true,
|
2022-11-24 15:10:21 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
throw new BadDataException('Project not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
throw new BadDataException('Project not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!project.paymentProviderCustomerId) {
|
|
|
|
throw new BadDataException(
|
|
|
|
'Payment Provider customer not found'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!project.paymentProviderSubscriptionId) {
|
|
|
|
throw new BadDataException(
|
|
|
|
'Payment Provider subscription not found'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body: JSONObject = req.body;
|
|
|
|
|
2022-11-24 15:13:21 +00:00
|
|
|
const item: BillingInvoice =
|
|
|
|
BaseModel.fromJSON<BillingInvoice>(
|
|
|
|
body['data'] as JSONObject,
|
|
|
|
this.entityType
|
|
|
|
) as BillingInvoice;
|
2022-11-24 15:10:21 +00:00
|
|
|
|
|
|
|
if (!item.paymentProviderInvoiceId) {
|
2022-11-24 15:13:21 +00:00
|
|
|
throw new BadDataException('Invoice ID not found');
|
2022-11-24 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!item.paymentProviderCustomerId) {
|
2022-11-24 15:13:21 +00:00
|
|
|
throw new BadDataException('Customer ID not found');
|
2022-11-24 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 15:13:21 +00:00
|
|
|
const invoice: Invoice = await BillingService.payInvoice(
|
|
|
|
item.paymentProviderCustomerId!,
|
|
|
|
item.paymentProviderInvoiceId!
|
|
|
|
);
|
2022-11-24 15:10:21 +00:00
|
|
|
|
2022-11-24 15:13:21 +00:00
|
|
|
// save updated status.
|
2022-11-24 15:10:21 +00:00
|
|
|
|
|
|
|
await this.service.updateOneBy({
|
|
|
|
query: {
|
2022-11-24 15:13:21 +00:00
|
|
|
paymentProviderInvoiceId: invoice.id!,
|
2022-11-24 15:10:21 +00:00
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
2022-11-24 15:13:21 +00:00
|
|
|
ignoreHooks: true,
|
2022-11-24 15:10:21 +00:00
|
|
|
},
|
|
|
|
data: {
|
2022-11-24 15:13:21 +00:00
|
|
|
status: invoice.status,
|
|
|
|
},
|
|
|
|
});
|
2022-11-24 15:10:21 +00:00
|
|
|
|
2022-11-24 15:13:21 +00:00
|
|
|
// refresh subscription status.
|
|
|
|
const subscriptionState =
|
|
|
|
await BillingService.getSubscriptionStatus(
|
|
|
|
project.paymentProviderSubscriptionId as string
|
|
|
|
);
|
2022-11-24 15:10:21 +00:00
|
|
|
|
|
|
|
await ProjectService.updateOneById({
|
|
|
|
id: project.id!,
|
|
|
|
data: {
|
2022-11-24 15:13:21 +00:00
|
|
|
paymentProviderSubscriptionStatus:
|
|
|
|
subscriptionState,
|
2022-11-24 15:10:21 +00:00
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
2022-11-24 15:13:21 +00:00
|
|
|
ignoreHooks: true,
|
|
|
|
},
|
2022-11-24 15:10:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return Response.sendEmptyResponse(req, res);
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|