65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import { Reception } from './classes.reception.js';
|
|
import { BillingPlan } from './classes.billingplan.js';
|
|
import * as plugins from '../plugins.js';
|
|
|
|
export class BillingPlanManager {
|
|
public receptionRef: Reception;
|
|
public get db() {
|
|
return this.receptionRef.db.smartdataDb;
|
|
}
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
|
|
|
public CBillingPlan = plugins.smartdata.setDefaultManagerForDoc(this, BillingPlan);
|
|
|
|
constructor(receptionRefArg: Reception) {
|
|
this.receptionRef = receptionRefArg;
|
|
this.receptionRef.typedrouter.addTypedRouter(this.typedrouter);
|
|
this.typedrouter.addTypedHandler(new plugins.typedrequest.TypedHandler<plugins.idpInterfaces.request.IReq_UpdatePaymentMethod>('updatePaymentMethod', async reqDataArg => {
|
|
const user = await this.receptionRef.userManager.getUserByJwt(reqDataArg.jwtString);
|
|
const organization = await this.receptionRef.organizationmanager.COrganization.getInstance({
|
|
id: reqDataArg.orgId,
|
|
});
|
|
const userIsAdmin = await organization.checkIfUserIsAdmin(user);
|
|
if (!userIsAdmin) {
|
|
throw new plugins.typedrequest.TypedResponseError('user is not an admin for the organization that the billing plan is for');
|
|
}
|
|
// ok user is admin
|
|
const newBillingPlan = new this.CBillingPlan();
|
|
newBillingPlan.id = plugins.smartunique.shortId();
|
|
newBillingPlan.data = {
|
|
type: 'Paddle',
|
|
proEnabled: false,
|
|
organizationId: reqDataArg.orgId,
|
|
status: 'active',
|
|
seats: 0,
|
|
alternativePaymentData: null,
|
|
billingEvents: [],
|
|
communications: [],
|
|
lastProcessed: Date.now(),
|
|
nextBilling: {
|
|
items: [],
|
|
method: 'paddle',
|
|
ontrack: true,
|
|
selectedBillingDate: Date.now(),
|
|
},
|
|
paddleData: {
|
|
checkoutId: reqDataArg.paddle?.checkoutId
|
|
}
|
|
}
|
|
await newBillingPlan.save();
|
|
return {
|
|
billingPlan: {
|
|
id: newBillingPlan.id,
|
|
data: {
|
|
type: newBillingPlan.data.type,
|
|
organizationId: newBillingPlan.data.organizationId,
|
|
proEnabled: newBillingPlan.data.proEnabled,
|
|
nextBilling: newBillingPlan.data.nextBilling,
|
|
billingEvents: newBillingPlan.data.billingEvents,
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
}
|