-
Notifications
You must be signed in to change notification settings - Fork 4
delete temporal schedule for discourse. #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -144,6 +144,15 @@ const updatePlatform = async ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns {Promise<HydratedDocument<IPlatform>>} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const deletePlatform = async (platform: HydratedDocument<IPlatform>): Promise<HydratedDocument<IPlatform>> => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (platform.name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case PlatformNames.Discourse: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (platform.metadata?.scheduleId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await discourseService.coreService.deleteDiscourseSchedule(platform.metadata.scheduleId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+147
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix case fallthrough and add error handling There are several issues in the platform deletion logic:
Apply this diff to fix the immediate issues: switch (platform.name) {
case PlatformNames.Discourse: {
if (platform.metadata?.scheduleId) {
- await discourseService.coreService.deleteDiscourseSchedule(platform.metadata.scheduleId);
+ try {
+ await discourseService.coreService.deleteDiscourseSchedule(platform.metadata.scheduleId);
+ } catch (error) {
+ throw new ApiError(
+ httpStatus.INTERNAL_SERVER_ERROR,
+ `Failed to delete Discourse schedule: ${error.message}`
+ );
+ }
}
+ break;
}
default: {
+ break;
}
}Consider refactoring to use a strategy pattern for platform-specific deletion logic as the number of platforms grows. This would make the code more maintainable and easier to test. Would you like me to provide an example implementation? 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 148-152: This case is falling through to the next case. Add a (lint/suspicious/noFallthroughSwitchClause) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await platform.remove(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -157,7 +166,7 @@ const deletePlatformByFilter = async (filter: object): Promise<HydratedDocument< | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!platform) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ApiError(httpStatus.NOT_FOUND, 'Platform not found'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await platform.remove(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return await deletePlatform(platform); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function getMetadataKey(platformName: string): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,18 @@ class TemporalDiscourseService extends TemporalCoreService { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(`Failed to create Temporal schedule: ${(error as Error).message}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public async pauseSchedule(scheduleId: string): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const client: Client = await this.getClient(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handle = client.schedule.getHandle(scheduleId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await handle.pause(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling and input validation. The method needs additional safeguards for production reliability:
Consider this improved implementation: + /**
+ * Pauses a Discourse schedule by its ID
+ * @param scheduleId - The ID of the schedule to pause
+ * @throws Error if the schedule cannot be paused or doesn't exist
+ */
public async pauseSchedule(scheduleId: string): Promise<void> {
+ if (!scheduleId) {
+ throw new Error('Schedule ID is required');
+ }
const client: Client = await this.getClient();
- const handle = client.schedule.getHandle(scheduleId);
- await handle.pause();
+ try {
+ const handle = client.schedule.getHandle(scheduleId);
+ await handle.pause();
+ } catch (error) {
+ throw new Error(`Failed to pause schedule ${scheduleId}: ${(error as Error).message}`);
+ }
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public async deleteSchedule(scheduleId: string): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const client: Client = await this.getClient(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handle = client.schedule.getHandle(scheduleId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await handle.delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance deletion method with error handling, validation, and logging. As this is the core functionality for the PR's objective of deleting temporal schedules, it needs to be robust and auditable. Consider this enhanced implementation: + /**
+ * Deletes a Discourse schedule by its ID
+ * @param scheduleId - The ID of the schedule to delete
+ * @throws Error if the schedule cannot be deleted or doesn't exist
+ */
public async deleteSchedule(scheduleId: string): Promise<void> {
+ if (!scheduleId) {
+ throw new Error('Schedule ID is required');
+ }
const client: Client = await this.getClient();
- const handle = client.schedule.getHandle(scheduleId);
- await handle.delete();
+ try {
+ const handle = client.schedule.getHandle(scheduleId);
+ // Verify schedule exists before deletion
+ await handle.describe();
+ await handle.delete();
+ console.info(`Successfully deleted Discourse schedule: ${scheduleId}`);
+ } catch (error) {
+ const message = `Failed to delete schedule ${scheduleId}: ${(error as Error).message}`;
+ console.error(message);
+ throw new Error(message);
+ }
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default new TemporalDiscourseService(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add input validation and standardize error code
The function implementation looks good but could benefit from these improvements:
async function deleteDiscourseSchedule(scheduleId: string): Promise<void> { + if (!scheduleId) { + throw new ApiError(400, 'Schedule ID is required'); + } try { await temporalDiscourse.deleteSchedule(scheduleId); } catch (error) { logger.error(error, 'Failed to delete discourse schedule.'); - throw new ApiError(590, 'Failed to delete discourse schedule.'); + throw new ApiError(503, 'Failed to delete discourse schedule.'); } }📝 Committable suggestion