Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/controllers/lago.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ensureOrgSubscribed } from "../services/lago.service.js";

const SUPPORTED_EVENTS = ["create_company", "register_company_and_user"];

const provisionOrg = async (req, res, next) => {
const { event, data } = req.body;

if (!SUPPORTED_EVENTS.includes(event)) {
req.statusCode = 400;
res.locals = { success: false, message: "org_id not found" };
return next();
Comment thread
Husainbw786 marked this conversation as resolved.
}

const org_id = data?.company?.id;

if (!org_id) {
req.statusCode = 400;
res.locals = { success: false, message: "org_id not found" };
return next();
}

const result = await ensureOrgSubscribed(String(org_id));

res.locals = {
success: true,
message: "Customer and subscription created successfully",
data: result
};
req.statusCode = 200;
return next();
};

export default { provisionOrg };
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import converstaionRoutes from "./routes/conversation.routes.js";
import internalRoutes from "./routes/internal.routes.js";
import promptWrapperRoutes from "./routes/promptWrapper.routes.js";
import richUiTemplateRoutes from "./routes/richUiTemplate.routes.js";
import lagoRoutes from "./routes/lago.routes.js";
const app = express();
configDotenv();
const PORT = process.env.PORT || 7072;
Expand Down Expand Up @@ -88,6 +89,7 @@ app.use("/api/template", templateRoute);
app.use("/api/prompt_wrappers", promptWrapperRoutes);
app.use("/api/internal", internalRoutes);
app.use("/api/rich_ui_templates", richUiTemplateRoutes);
app.use("/api/lago", lagoRoutes);

//Metrics
// app.use('/api/v1/metrics', metrisRoutes);
Expand Down
11 changes: 11 additions & 0 deletions src/routes/lago.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from "express";
import lagoController from "../controllers/lago.controller.js";

const router = express.Router();

// POST /api/lago/provision
// Body: { org_id: string }
// Creates a Lago customer and assigns a subscription for the given org
router.post("/provision", lagoController.provisionOrg);

export default router;
46 changes: 46 additions & 0 deletions src/services/lago.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import axios from "axios";

const LAGO_API_URL = process.env.LAGO_API_URL;
const LAGO_API_KEY = process.env.LAGO_API_KEY;
const LAGO_FREE_PLAN_CODE = process.env.LAGO_FREE_PLAN_CODE;

const lagoHeaders = () => ({
Authorization: `Bearer ${LAGO_API_KEY}`,
"Content-Type": "application/json"
});

export const createCustomer = async (org_id) => {
const response = await axios.post(
`${LAGO_API_URL}/customers`,
{
customer: {
external_id: org_id,
name: org_id
}
},
{ headers: lagoHeaders() }
);
return response.data;
};
Comment thread
Husainbw786 marked this conversation as resolved.

export const createSubscription = async (org_id) => {
const response = await axios.post(
`${LAGO_API_URL}/subscriptions`,
{
subscription: {
external_customer_id: org_id,
plan_code: LAGO_FREE_PLAN_CODE,
external_id: org_id,
billing_time: "calendar"
}
},
{ headers: lagoHeaders() }
);
return response.data;
};

export const ensureOrgSubscribed = async (org_id) => {
const customer = await createCustomer(org_id);
const subscription = await createSubscription(org_id);
return { customer, subscription };
};
Comment thread
Husainbw786 marked this conversation as resolved.