Skip to content

Commit

Permalink
clean up action fields before saving to db
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 25, 2025
1 parent 523d425 commit f9f69bf
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/web/app/(app)/automation/RuleForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
type CreateRuleBody,
createRuleBody,
} from "@/utils/actions/validation";
import { actionInputs } from "@/utils/actionType";
import { actionInputs } from "@/utils/action-item";
import { Select } from "@/components/Select";
import { Toggle } from "@/components/Toggle";
import type { GroupsResponse } from "@/app/api/user/group/route";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/email-list/PlanExplanation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Badge } from "@/components/Badge";
import type { Thread } from "@/components/email-list/types";
import { PlanActions } from "@/components/email-list/PlanActions";
import { PlanBadge, getActionColor } from "@/components/PlanBadge";
import { getActionFields } from "@/utils/actionType";
import { getActionFields } from "@/utils/action-item";

export function PlanExplanation(props: {
thread: Thread;
Expand Down
86 changes: 85 additions & 1 deletion apps/web/utils/actionType.ts → apps/web/utils/action-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type Action, ActionType, type ExecutedAction } from "@prisma/client";
import {
type Action,
ActionType,
type ExecutedAction,
type Prisma,
} from "@prisma/client";

export const actionInputs: Record<
ActionType,
Expand Down Expand Up @@ -140,3 +145,82 @@ export function getActionFields(fields: Action | ExecutedAction | undefined) {

return res;
}

type ActionFieldsSelection = Pick<
Prisma.ActionCreateInput,
"type" | "label" | "subject" | "content" | "to" | "cc" | "bcc" | "url"
>;

export function sanitizeActionFields(
action: Partial<ActionFieldsSelection> & { type: ActionType },
): ActionFieldsSelection {
const base = {
type: action.type,
label: null,
subject: null,
content: null,
to: null,
cc: null,
bcc: null,
url: null,
};

switch (action.type) {
case ActionType.ARCHIVE:
case ActionType.MARK_SPAM:
return base;
case ActionType.LABEL: {
return {
...base,
label: action.label ?? null,
};
}
case ActionType.REPLY: {
return {
...base,
content: action.content ?? null,
cc: action.cc ?? null,
bcc: action.bcc ?? null,
};
}
case ActionType.SEND_EMAIL: {
return {
...base,
subject: action.subject ?? null,
content: action.content ?? null,
to: action.to ?? null,
cc: action.cc ?? null,
bcc: action.bcc ?? null,
};
}
case ActionType.FORWARD: {
return {
...base,
content: action.content ?? null,
to: action.to ?? null,
cc: action.cc ?? null,
bcc: action.bcc ?? null,
};
}
case ActionType.DRAFT_EMAIL: {
return {
...base,
subject: action.subject ?? null,
content: action.content ?? null,
to: action.to ?? null,
cc: action.cc ?? null,
bcc: action.bcc ?? null,
};
}
case ActionType.CALL_WEBHOOK: {
return {
...base,
url: action.url ?? null,
};
}
default:
// biome-ignore lint/correctness/noSwitchDeclarations: intentional exhaustive check
const exhaustiveCheck: never = action.type;
return exhaustiveCheck;
}
}
35 changes: 20 additions & 15 deletions apps/web/utils/actions/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
appendRulePrompt,
} from "@/utils/rule/prompt-file";
import { generatePromptOnDeleteRule } from "@/utils/ai/rule/generate-prompt-on-delete-rule";
import { sanitizeActionFields } from "@/utils/action-item";

export const createRuleAction = withActionInstrumentation(
"createRule",
Expand All @@ -49,7 +50,7 @@ export const createRuleAction = withActionInstrumentation(
createMany: {
data: body.actions.map(
({ type, label, subject, content, to, cc, bcc, url }) => {
return {
return sanitizeActionFields({
type,
label: label?.value,
subject: subject?.value,
Expand All @@ -58,7 +59,7 @@ export const createRuleAction = withActionInstrumentation(
cc: cc?.value,
bcc: bcc?.value,
url: url?.value,
};
});
},
),
},
Expand Down Expand Up @@ -170,7 +171,7 @@ export const updateRuleAction = withActionInstrumentation(
...actionsToUpdate.map((a) => {
return prisma.action.update({
where: { id: a.id },
data: {
data: sanitizeActionFields({
type: a.type,
label: a.label?.value,
subject: a.subject?.value,
Expand All @@ -179,24 +180,28 @@ export const updateRuleAction = withActionInstrumentation(
cc: a.cc?.value,
bcc: a.bcc?.value,
url: a.url?.value,
},
}),
});
}),
// create new actions
...(actionsToCreate.length
? [
prisma.action.createMany({
data: actionsToCreate.map((a) => ({
ruleId: body.id,
type: a.type,
label: a.label?.value,
subject: a.subject?.value,
content: a.content?.value,
to: a.to?.value,
cc: a.cc?.value,
bcc: a.bcc?.value,
url: a.url?.value,
})),
data: actionsToCreate.map((a) => {
return {
...sanitizeActionFields({
type: a.type,
label: a.label?.value,
subject: a.subject?.value,
content: a.content?.value,
to: a.to?.value,
cc: a.cc?.value,
bcc: a.bcc?.value,
url: a.url?.value,
}),
ruleId: body.id,
};
}),
}),
]
: []),
Expand Down
13 changes: 2 additions & 11 deletions apps/web/utils/ai/choose-rule/run-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getEmailFromMessage } from "@/utils/ai/choose-rule/get-email-from-messa
import prisma from "@/utils/prisma";
import { createScopedLogger } from "@/utils/logger";
import type { MatchReason } from "@/utils/ai/choose-rule/types";
import { sanitizeActionFields } from "@/utils/action-item";

const logger = createScopedLogger("ai-run-rules");

Expand Down Expand Up @@ -169,17 +170,7 @@ async function saveExecutedRule(
const data: Prisma.ExecutedRuleCreateInput = {
actionItems: {
createMany: {
data:
actionItems?.map((a) => ({
type: a.type,
label: a.label,
subject: a.subject,
content: a.content,
to: a.to,
cc: a.cc,
bcc: a.bcc,
url: a.url,
})) || [],
data: actionItems?.map(sanitizeActionFields) || [],
},
},
messageId,
Expand Down

1 comment on commit f9f69bf

@vercel
Copy link

@vercel vercel bot commented on f9f69bf Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.