Skip to content

Open-source status page, incident response and uptime monitoring

🚫 limit sms per workspace (#1326)

Thibault Le Ouayab8c37c61678
diff --git a/apps/workflows/src/checker/alerting.ts b/apps/workflows/src/checker/alerting.ts
index 8ff4ba0eb..f780bddb0 100644
--- a/apps/workflows/src/checker/alerting.ts
+++ b/apps/workflows/src/checker/alerting.ts
@@ -1,8 +1,9 @@
-import { db, eq, schema } from "@openstatus/db";
+import { and, count, db, eq, gte, inArray, schema } from "@openstatus/db";
import type { MonitorStatus } from "@openstatus/db/src/schema";
import {
selectMonitorSchema,
selectNotificationSchema,
+ selectWorkspaceSchema,
} from "@openstatus/db/src/schema";
import type {
@@ -46,6 +47,58 @@ export const triggerNotifications = async ({
.where(eq(schema.monitor.id, Number(monitorId)))
.all();
for (const notif of notifications) {
+ // for sms check we are in the quota
+ if (notif.notification.provider === "sms") {
+ if (notif.notification.workspaceId === null) {
+ continue;
+ }
+
+ const workspace = await db
+ .select()
+ .from(schema.workspace)
+ .where(eq(schema.workspace.id, notif.notification.workspaceId));
+
+ if (workspace.length !== 1) {
+ continue;
+ }
+
+ const data = selectWorkspaceSchema.parse(workspace[0]);
+
+ const oneMonthAgo = new Date();
+ oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
+
+ const smsNotification = await db
+ .select()
+ .from(schema.notification)
+ .where(
+ and(
+ eq(schema.notification.workspaceId, notif.notification.workspaceId),
+ eq(schema.notification.provider, "sms"),
+ ),
+ );
+ const ids = smsNotification.map((notification) => notification.id);
+
+ const smsSent = await db
+ .select({ count: count() })
+ .from(schema.notificationTrigger)
+ .where(
+ and(
+ gte(
+ schema.notificationTrigger.cronTimestamp,
+ Math.floor(oneMonthAgo.getTime() / 1000),
+ ),
+ inArray(schema.notificationTrigger.notificationId, ids),
+ ),
+ )
+ .all();
+
+ if ((smsSent[0]?.count ?? 0) > data.limits["sms-limit"]) {
+ console.log(
+ `SMS quota exceeded for workspace ${notif.notification.workspaceId}`,
+ );
+ continue;
+ }
+ }
console.log(
`💌 sending notification for ${monitorId} and chanel ${notif.notification.provider} for ${notifType}`,
);
diff --git a/packages/db/src/schema/plan/config.ts b/packages/db/src/schema/plan/config.ts
index ced68df9d..2343e0e4d 100644
--- a/packages/db/src/schema/plan/config.ts
+++ b/packages/db/src/schema/plan/config.ts
@@ -44,6 +44,7 @@ export const allPlans: Record<
"white-label": false,
notifications: true,
sms: false,
+ "sms-limit": 0,
pagerduty: false,
opsgenie: false,
"notification-channels": 1,
@@ -83,6 +84,7 @@ export const allPlans: Record<
pagerduty: true,
opsgenie: true,
sms: true,
+ "sms-limit": 50,
"notification-channels": 10,
members: "Unlimited",
"audit-log": false,
@@ -154,6 +156,7 @@ export const allPlans: Record<
"white-label": false,
notifications: true,
sms: true,
+ "sms-limit": 100,
pagerduty: true,
opsgenie: true,
"notification-channels": 20,
diff --git a/packages/db/src/schema/plan/schema.ts b/packages/db/src/schema/plan/schema.ts
index 5b55be481..f1ff06f55 100644
--- a/packages/db/src/schema/plan/schema.ts
+++ b/packages/db/src/schema/plan/schema.ts
@@ -41,6 +41,7 @@ export const limitsSchema = z.object({
pagerduty: z.boolean().default(false),
opsgenie: z.boolean().default(false),
sms: z.boolean().default(false),
+ "sms-limit": z.number().default(0),
"notification-channels": z.number().default(1),
/**
* Collaboration limits