@dbbs/strapi-stripe-payment
Version:
Strapi integration plugin for Stripe payment system
146 lines (145 loc) • 7.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const http_errors_1 = __importDefault(require("http-errors"));
const strapi_1 = require("@strapi/strapi");
const validationSchemas_1 = require("../validationSchemas");
const helpers_1 = require("../helpers");
exports.default = strapi_1.factories.createCoreController('plugin::stripe-payment.organization', ({ strapi }) => ({
async create(ctx) {
const { name, email, quantity } = ctx.request.body;
const { user: { id: ownerId } } = ctx.state;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.createOrganizationSchema, { name, email, quantity });
const organization = await strapi
.plugin('stripe-payment')
.service('organization')
.create({
ownerId,
...validatedParams
});
if (!organization) {
throw new http_errors_1.default.NotFound(`Cannot create an organization as the owner with ID ${ownerId} was not found`);
}
ctx.send(organization);
},
async createByAdmin(ctx) {
const { name, ownerId, email, quantity } = ctx.request.body;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.createOrganizationByAdminSchema, {
name,
ownerId,
email,
quantity
});
const organization = await strapi.plugin('stripe-payment').service('organization').create(validatedParams);
if (!organization) {
throw new http_errors_1.default.NotFound(`Cannot create an organization as the owner with ID ${ownerId} was not found`);
}
ctx.send(organization);
},
async getOrganizationById(ctx) {
const { id } = ctx.params;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.getOrganizationByIdSchema, { id });
const organization = await strapi
.plugin('stripe-payment')
.service('organization')
.getOrganizationById(validatedParams);
if (!organization) {
throw new http_errors_1.default.NotFound(`Organization with ID ${id} was not found`);
}
ctx.send(organization);
},
async getUserOrganizations(ctx) {
const { user } = ctx.state;
const organizations = await strapi.plugin('stripe-payment').service('organization').getUserOrganizations({
userId: user.id
});
ctx.send(organizations);
},
async getAllOrganizations(ctx) {
const organizations = await strapi.plugin('stripe-payment').service('organization').getAllOrganizations();
ctx.send(organizations);
},
async getDefaultPaymentMethod(ctx) {
const { id } = ctx.params;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.getDefaultPaymentMethodSchema, { id });
const paymentMethod = await strapi
.plugin('stripe-payment')
.service('organization')
.getDefaultPaymentMethod(validatedParams);
if (!paymentMethod) {
throw new http_errors_1.default.NotFound(`Organization with ID ${id} was not found`);
}
ctx.send(paymentMethod);
},
async update(ctx) {
const { id } = ctx.params;
const { quantity, name } = ctx.request.body;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.updateOrganizationSchema, { id, quantity, name });
const organization = await strapi.plugin('stripe-payment').service('organization').update(validatedParams);
if (!organization) {
throw new http_errors_1.default.NotFound(`Organization with ID ${id} not found`);
}
ctx.send(organization);
},
async delete(ctx) {
const { id } = ctx.params;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.deleteOrganizationSchema, { id });
const result = await strapi.plugin('stripe-payment').service('organization').delete(validatedParams);
if (!result) {
throw new http_errors_1.default.NotFound(`Organization with ID ${id} was not found`);
}
ctx.send(result);
},
async updateOwner(ctx) {
const { id } = ctx.params;
const { ownerId } = ctx.request.body;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.updateOwnerSchema, { id, ownerId });
const organization = await strapi.plugin('stripe-payment').service('organization').updateOwner(validatedParams);
if (!organization) {
throw new http_errors_1.default.NotFound(`Organization with ID ${id} or user with ID ${ownerId} was not found`);
}
ctx.send(organization);
},
async createDefaultPaymentMethodUpdateCheckoutSession(ctx) {
const { id } = ctx.params;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.updateDefaultPaymentMethodSchema, { id });
const checkoutSession = await strapi
.plugin('stripe-payment')
.service('organization')
.createDefaultPaymentMethodUpdateCheckoutSession(validatedParams);
if (!checkoutSession) {
throw new http_errors_1.default.NotFound(`Organization with ID ${id} was not found`);
}
ctx.send(checkoutSession);
},
async addUser(ctx) {
const { id: organizationId } = ctx.params;
const { recipientEmail } = ctx.request.body;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.addUserSchema, { organizationId, recipientEmail });
const organization = await strapi.plugin('stripe-payment').service('organization').addUser(validatedParams);
if (!organization) {
throw new http_errors_1.default.NotFound(`Organization with ID ${organizationId} was not found`);
}
ctx.send(organization);
},
async removeUser(ctx) {
const { id: organizationId } = ctx.params;
const { userId } = ctx.request.body;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.removeUserSchema, { organizationId, userId });
const organization = await strapi.plugin('stripe-payment').service('organization').removeUser(validatedParams);
if (!organization) {
throw new http_errors_1.default.NotFound(`Organization with ID ${organizationId} was not found`);
}
ctx.send(organization);
},
async acceptInvite(ctx) {
const { id: organizationId } = ctx.params;
const { user } = ctx.state;
const { token } = ctx.request.body;
const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.acceptInviteSchema, { organizationId, userId: user.id, token });
const result = await strapi.plugin('stripe-payment').service('organization').acceptInvite(validatedParams);
ctx.send(result);
}
}));