mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-23 07:42:10 +00:00
85 lines
3.2 KiB
TypeScript
85 lines
3.2 KiB
TypeScript
|
|
import BadDataException from 'Common/Types/Exception/BadDataException';
|
|
import Permission from 'Common/Types/Permission';
|
|
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';
|
|
import { ExpressRequest, ExpressResponse, NextFunction } from '../Utils/Express';
|
|
import Response from '../Utils/Response';
|
|
import BaseAPI from './BaseAPI';
|
|
|
|
export default class UserAPI extends BaseAPI<BillingPaymentMethod, BillingPaymentMethodServiceType> {
|
|
public constructor() {
|
|
super(BillingPaymentMethod, BillingPaymentMethodService);
|
|
|
|
this.router.post(
|
|
`/${new this.entityType().getCrudApiPath()?.toString()}/setup`,
|
|
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'
|
|
);
|
|
}
|
|
|
|
const userPermissions = this.getPermissionsForTenant(req);
|
|
|
|
if (userPermissions.filter((permission) => {
|
|
permission.permission === Permission.ProjectOwner
|
|
}).length === 0) {
|
|
throw new BadDataException("Only Project owner can add payment methods.");
|
|
}
|
|
|
|
const project: Project | null = await ProjectService.findOneById({
|
|
id: this.getDatabaseCommonInteractionProps(req).tenantId!,
|
|
props: {
|
|
isRoot: true
|
|
},
|
|
select: {
|
|
_id: true,
|
|
paymentProviderCustomerId: true
|
|
}
|
|
});
|
|
|
|
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");
|
|
}
|
|
|
|
const setupIntent: string = await BillingService.getSetupIntentSecret(project.paymentProviderCustomerId);
|
|
|
|
return Response.sendJsonObjectResponse(req, res, {
|
|
setupIntent: setupIntent,
|
|
});
|
|
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|