oneuptime/CommonServer/API/BillingPaymentMethodAPI.ts

105 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-11-17 15:11:23 +00:00
import BadDataException from 'Common/Types/Exception/BadDataException';
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(
`/${new this.entityType().getCrudApiPath()?.toString()}/setup`,
UserMiddleware.getUserMiddleware,
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
) => {
try {
if (!IsBillingEnabled) {
2022-11-17 22:13:13 +00:00
throw new BadDataException(
'Billign is not enabled for this server'
);
2022-11-17 15:11:23 +00:00
}
if (req.body['projectId']) {
throw new BadDataException(
'projectId is required in request body'
);
}
2022-11-17 22:13:13 +00:00
const userPermissions = this.getPermissionsForTenant(
req
).filter((permission) => {
2022-11-17 19:41:53 +00:00
console.log(permission.permission);
//FIX: Change "Project"
2022-11-17 22:13:13 +00:00
return (
permission.permission.toString() === 'ProjectOwner'
);
});
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({
id: this.getDatabaseCommonInteractionProps(req)
.tenantId!,
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);
}
}
);
}
}