Get the entitlement before triggering a non-dev task

This commit is contained in:
Matt Aitken
2024-07-03 17:46:55 +01:00
parent b8852ecca0
commit aeca06457b
2 changed files with 35 additions and 0 deletions
@@ -214,6 +214,27 @@ export async function reportComputeUsage(request: Request) {
body: await request.text(),
});
}
export async function getEntitlement(organizationId: string) {
const client = getClient();
if (!client) return undefined;
try {
const result = await client.getEntitlement(organizationId);
if (!result.success) {
logger.error("Error getting entitlement", { error: result.error });
return {
hasAccess: true as const,
};
}
return result;
} catch (e) {
logger.error("Error getting entitlement", { error: e });
return {
hasAccess: true as const,
};
}
}
export async function projectCreated(organization: Organization, project: Project) {
if (project.version === "V2" || !isCloud()) {
await createEnvironment(organization, project, "STAGING");
@@ -12,6 +12,7 @@ import { eventRepository } from "../eventRepository.server";
import { generateFriendlyId } from "../friendlyIdentifiers";
import { uploadToObjectStore } from "../r2.server";
import { BaseService } from "./baseService.server";
import { getEntitlement } from "~/services/platform.v3.server";
export type TriggerTaskServiceOptions = {
idempotencyKey?: string;
@@ -23,6 +24,12 @@ export type TriggerTaskServiceOptions = {
customIcon?: string;
};
export class OutOfEntitlementError extends Error {
constructor() {
super("You can't trigger a task because you have run out of credits.");
}
}
export class TriggerTaskService extends BaseService {
public async call(
taskId: string,
@@ -51,6 +58,13 @@ export class TriggerTaskService extends BaseService {
return existingRun;
}
if (environment.type !== "DEVELOPMENT") {
const result = await getEntitlement(environment.organizationId);
if (result && result.hasAccess === false) {
throw new OutOfEntitlementError();
}
}
return await eventRepository.traceEvent(
taskId,
{