-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fallback to label name if label id not found #895
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d92f7a1
logger
elie222 4f3e9a4
Fallback to label name if label id not found
elie222 17a8c18
fallback to label name
elie222 676ea4c
fallback to label name if id is wrong
elie222 94443f8
Merge branch 'main' into feat/fallback-to-name
elie222 42ee020
fix logs
elie222 c2cb81d
fix tests
elie222 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import type { EmailProvider } from "@/utils/email/types"; | ||
| import { createScopedLogger } from "@/utils/logger"; | ||
| import prisma from "@/utils/prisma"; | ||
|
|
||
| /** | ||
| * Labels a message and automatically updates the database if a stale label ID was detected and fixed. | ||
| * | ||
| * This handles the case where labels/categories are deleted and recreated with new IDs: | ||
| * - Tries to label with the provided ID | ||
| * - If that fails and labelName is provided, falls back to looking up by name | ||
| * - If the actual ID used differs from the stored ID, updates ALL Actions with that stale ID | ||
| */ | ||
| export async function labelMessageAndSync({ | ||
| provider, | ||
| messageId, | ||
| labelId, | ||
| labelName, | ||
| emailAccountId, | ||
| }: { | ||
| provider: EmailProvider; | ||
| messageId: string; | ||
| labelId: string; | ||
| labelName: string | null; | ||
| emailAccountId: string; | ||
| }): Promise<void> { | ||
| const logger = createScopedLogger("label.server").with({ | ||
| provider: provider.name, | ||
| messageId, | ||
| labelId, | ||
| labelName, | ||
| emailAccountId, | ||
| }); | ||
|
|
||
| const result = await provider.labelMessage({ | ||
| messageId, | ||
| labelId, | ||
| labelName, | ||
| }); | ||
|
|
||
| // If we had to use fallback and got a different ID, update all Actions with the stale ID | ||
| if ( | ||
| result.usedFallback && | ||
| result.actualLabelId && | ||
| result.actualLabelId !== labelId | ||
| ) { | ||
| logger.info("Detected stale label ID, updating all instances in database", { | ||
| oldLabelId: labelId, | ||
| newLabelId: result.actualLabelId, | ||
| }); | ||
|
|
||
| try { | ||
| const updateResult = await prisma.action.updateMany({ | ||
| where: { | ||
| labelId, | ||
| rule: { emailAccountId }, | ||
| }, | ||
| data: { labelId: result.actualLabelId }, | ||
| }); | ||
|
|
||
| logger.info("Updated stale label IDs across all actions", { | ||
| newLabelId: result.actualLabelId, | ||
| updatedCount: updateResult.count, | ||
| }); | ||
| } catch (error) { | ||
| // Don't fail the whole operation if DB update fails | ||
| logger.error("Failed to update stale label IDs", { | ||
| newLabelId: result.actualLabelId, | ||
| error, | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.