oneuptime/CommonServer/API/BillingPaymentMethodAPI.ts

106 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-17 15:11:23 +00:00
import BadDataException from 'Common/Types/Exception/BadDataException';
2022-11-25 14:57:58 +00:00
import Permission, { UserPermission } from 'Common/Types/Permission';
2022-11-17 15:11:23 +00:00
import BillingPaymentMethod from 'Model/Models/BillingPaymentMethod';
import Project from 'Model/Models/Project';
import { IsBillingEnabled } from '../Config';
import UserMiddleware from '../Middleware/UserAuthorization';
import BillingPaymentMethodService, {
Service as BillingPaymentMethodServiceType,
} from '../Services/BillingPaymentMethodService';
import BillingService from '../Services/BillingService';
import ProjectService from '../Services/ProjectService';
2022-11-17 22:13:13 +00:00
import {
ExpressRequest,
ExpressResponse,
NextFunction,
} from '../Utils/Express';
2022-11-17 15:11:23 +00:00
import Response from '../Utils/Response';
import BaseAPI from './BaseAPI';
2022-11-17 22:13:13 +00:00
export default class UserAPI extends BaseAPI<
BillingPaymentMethod,
BillingPaymentMethodServiceType
> {
2022-11-17 15:11:23 +00:00
public constructor() {
super(BillingPaymentMethod, BillingPaymentMethodService);
this.router.post(
2022-12-20 07:13:36 +00:00
`${new this.entityType().getCrudApiPath()?.toString()}/setup`,
2022-11-17 15:11:23 +00:00
UserMiddleware.getUserMiddleware,
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
) => {
try {
if (!IsBillingEnabled) {
2022-11-17 22:13:13 +00:00
throw new BadDataException(
2023-07-30 21:29:50 +00:00
'Billing is not enabled for this server'
2022-11-17 22:13:13 +00:00
);
2022-11-17 15:11:23 +00:00
}
if (req.body['projectId']) {
throw new BadDataException(
'projectId is required in request body'
);
}
2022-11-25 14:57:58 +00:00
const userPermissions: Array<UserPermission> = (
2022-11-24 15:13:21 +00:00
await this.getPermissionsForTenant(req)
2022-11-25 14:57:58 +00:00
).filter((permission: UserPermission) => {
2022-11-17 22:13:13 +00:00
return (
2022-11-24 15:13:21 +00:00
permission.permission.toString() ===
Permission.ProjectOwner.toString() ||
permission.permission.toString() ===
Permission.CanCreateBillingPaymentMethod.toString()
2022-11-17 22:13:13 +00:00
);
});
2022-11-17 15:11:23 +00:00
2022-11-17 19:41:53 +00:00
if (userPermissions.length === 0) {
2022-11-17 22:13:13 +00:00
throw new BadDataException(
'Only Project owner can add payment methods.'
);
2022-11-17 15:11:23 +00:00
}
2022-11-17 22:13:13 +00:00
const project: Project | null =
await ProjectService.findOneById({
2022-11-22 20:18:12 +00:00
id: this.getTenantId(req)!,
2022-11-17 22:13:13 +00:00
props: {
isRoot: true,
},
select: {
_id: true,
paymentProviderCustomerId: true,
},
});
2022-11-17 15:11:23 +00:00
if (!project) {
2022-11-17 22:13:13 +00:00
throw new BadDataException('Project not found');
2022-11-17 15:11:23 +00:00
}
if (!project) {
2022-11-17 22:13:13 +00:00
throw new BadDataException('Project not found');
2022-11-17 15:11:23 +00:00
}
if (!project.paymentProviderCustomerId) {
2022-11-17 22:13:13 +00:00
throw new BadDataException(
'Payment Provider customer not found'
);
2022-11-17 15:11:23 +00:00
}
2022-11-17 22:13:13 +00:00
const setupIntent: string =
await BillingService.getSetupIntentSecret(
project.paymentProviderCustomerId
);
2022-11-17 15:11:23 +00:00
return Response.sendJsonObjectResponse(req, res, {
setupIntent: setupIntent,
});
} catch (err) {
next(err);
}
}
);
}
}