Added transaction to creating an SES setting & upgraded to Prisma ORM 6 (#109)

* Added transaction to creating SES setting & upgraded Prisma to Prisma ORM 6

* Keep logging of the queue status in

---------

Co-authored-by: Kumarion <121711454+Kumarion@users.noreply.github.com>
This commit is contained in:
Arxk
2025-03-08 01:29:52 -05:00
committed by KM Koushik
parent d7239d6616
commit 9c5da6dca6
9 changed files with 160 additions and 426 deletions

View File

@@ -78,31 +78,64 @@ export class SesSettingsService {
}
const idPrefix = smallNanoid(10);
let topicArn: string | undefined;
const setting = await db.sesSetting.create({
data: {
try {
const topicName = `${idPrefix}-${region}-unsend`;
topicArn = await sns.createTopic(topicName, region);
if (!topicArn) {
throw new Error("Failed to create SNS topic");
}
const setting = await db.$transaction(async (tx) => {
const setting = await tx.sesSetting.create({
data: {
region,
callbackUrl: `${parsedUrl}/api/ses_callback`,
topic: topicName,
topicArn,
sesEmailRateLimit: sendingRateLimit,
transactionalQuota,
idPrefix,
},
});
await sns.subscribeEndpoint(
topicArn!,
`${setting.callbackUrl}`,
setting.region
);
return setting;
});
if (!setting) {
throw new Error("Failed to create setting");
}
await registerConfigurationSet(setting);
EmailQueueService.initializeQueue(
region,
callbackUrl: `${parsedUrl}/api/ses_callback`,
topic: `${idPrefix}-${region}-unsend`,
sesEmailRateLimit: sendingRateLimit,
transactionalQuota,
idPrefix,
},
});
setting.sesEmailRateLimit,
setting.transactionalQuota
);
console.log(
EmailQueueService.transactionalQueue,
EmailQueueService.marketingQueue
);
await createSettingInAws(setting);
EmailQueueService.initializeQueue(
region,
setting.sesEmailRateLimit,
setting.transactionalQuota
);
console.log(
EmailQueueService.transactionalQueue,
EmailQueueService.marketingQueue
);
await this.invalidateCache();
await this.invalidateCache();
} catch (error) {
if (topicArn) {
try {
await sns.deleteTopic(topicArn, region);
} catch (deleteError) {
console.error('Failed to delete SNS topic after error:', deleteError);
}
}
throw error;
}
}
public static async updateSesSetting({
@@ -163,41 +196,6 @@ export class SesSettingsService {
}
}
async function createSettingInAws(setting: SesSetting) {
await registerTopicInAws(setting).then(registerConfigurationSet);
}
/**
* Creates a new topic in AWS and subscribes the callback URL to it
*/
async function registerTopicInAws(setting: SesSetting) {
const topicArn = await sns.createTopic(setting.topic, setting.region);
if (!topicArn) {
throw new Error("Failed to create SNS topic");
}
const _setting = await db.sesSetting.update({
where: {
id: setting.id,
},
data: {
topicArn,
},
});
// Invalidate the cache to update the topicArn list
SesSettingsService.invalidateCache();
await sns.subscribeEndpoint(
topicArn,
`${setting.callbackUrl}`,
setting.region
);
return _setting;
}
/**
* Creates a new configuration set in AWS for given region
* Totally consist of 4 configs.