import * as z from "zod" import { defineAction } from "../../../automation/actions" import { findLabelId, getGmailApi, toGmailLabel, } from "@automate.ax/integration-contracts/gmail" import { GMAIL_LABELS_WRITE_REQUIREMENT } from "../lib/scopes" import { LABEL_COLOR_SCHEMA, LABEL_LIST_VISIBILITY_SCHEMA, LABEL_SCHEMA, MESSAGE_LIST_VISIBILITY_SCHEMA, } from "@automate.ax/integration-contracts/gmail" /** Updates selected metadata on a user-owned Gmail label. */ export const updateGmailLabel = defineAction("Update Gmail label") .describe("Updates a user-owned Gmail label.") .account("google", GMAIL_LABELS_WRITE_REQUIREMENT) .input( z .object({ /** Optional supported Gmail label color. */ color: LABEL_COLOR_SCHEMA.optional(), /** Gmail label display name or immutable ID to update. */ label: z.string().min(1), /** Visibility in Gmail's label list. */ labelListVisibility: LABEL_LIST_VISIBILITY_SCHEMA.optional(), /** Visibility beside messages in Gmail's message list. */ messageListVisibility: MESSAGE_LIST_VISIBILITY_SCHEMA.optional(), /** Optional replacement label name. */ name: z.string().min(1).optional(), }) .refine( ({ color, labelListVisibility, messageListVisibility, name }) => Boolean( color || labelListVisibility || messageListVisibility || name, ), "At least one label property must be updated.", ), ) .output(LABEL_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const gmailApi = getGmailApi(account.secret) const { data: label } = await gmailApi.users.labels.patch({ id: await findLabelId(gmailApi, input.label), requestBody: { color: input.color, labelListVisibility: input.labelListVisibility, messageListVisibility: input.messageListVisibility, name: input.name, }, userId: "me", }) return toGmailLabel(label) })