Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dist/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.open-next/

# TypeScript
*.tsbuildinfo
Expand Down
5 changes: 5 additions & 0 deletions claim-db-worker/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
37 changes: 23 additions & 14 deletions create-db-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,39 @@ export default {
return new Response('Missing region or name in request body', { status: 400 });
}

const payload = JSON.stringify({ region, name });
const prismaResponse = await fetch('https://api.prisma.io/v1/projects', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.INTEGRATION_TOKEN}`,
},
body: payload,
body: JSON.stringify({
region,
name,
}),
});

const prismaText = await prismaResponse.text();

// Trigger delete workflow for the new project
try {
const response = JSON.parse(prismaText);
const projectID = response.data ? response.data.id : response.id;
await env.DELETE_DB_WORKFLOW.create({ params: { projectID } });
env.CREATE_DB_DATASET.writeDataPoint({
blobs: ['database_created'],
indexes: ['create_db'],
});
} catch (e) {
console.error('Error parsing prismaText or triggering workflow:', e);
}
const backgroundTasks = async () => {
try {
const response = JSON.parse(prismaText);
const projectID = response.data ? response.data.id : response.id;

const workflowPromise = env.DELETE_DB_WORKFLOW.create({ params: { projectID } });

const analyticsPromise = env.CREATE_DB_DATASET.writeDataPoint({
blobs: ['database_created'],
indexes: ['create_db'],
});

await Promise.all([workflowPromise, analyticsPromise]);
} catch (e) {
console.error('Error in background tasks:', e);
}
};

ctx.waitUntil(backgroundTasks());

return new Response(prismaText, {
status: prismaResponse.status,
Expand Down
Loading