Skip to content

Commit

Permalink
Delivery type
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 22, 2024
1 parent cfbf44c commit 3d1fa53
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
13 changes: 10 additions & 3 deletions apps/dashboard/jobs/tasks/invoice/email/send-email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ export const sendInvoiceEmail = schemaTask({
return;
}

const customerEmail = invoice?.customer?.email;

if (!customerEmail) {
logger.error("Invoice customer email not found");
return;
}

const response = await resend.emails.send({
from: "Midday <[email protected]>",
to: invoice?.customer.email,
replyTo: invoice?.team.email,
to: customerEmail,
replyTo: invoice?.team.email ?? undefined,
subject: `${invoice?.team.name} sent you an invoice`,
headers: {
"X-Entity-Ref-ID": nanoid(),
Expand All @@ -58,7 +65,7 @@ export const sendInvoiceEmail = schemaTask({
.from("invoices")
.update({
status: "unpaid",
sent_to: invoice?.customer.email,
sent_to: customerEmail,
})
.eq("id", invoiceId);
},
Expand Down
75 changes: 37 additions & 38 deletions apps/dashboard/src/actions/invoice/create-invoice-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,50 @@
import { authActionClient } from "@/actions/safe-action";
import { tasks } from "@trigger.dev/sdk/v3";
import { revalidateTag } from "next/cache";
import { type InvoiceTemplate, createInvoiceSchema } from "./schema";
import { createInvoiceSchema } from "./schema";

export const createInvoiceAction = authActionClient
.metadata({
name: "create-invoice",
})
.schema(createInvoiceSchema)
.action(async ({ parsedInput: { id }, ctx: { supabase, user } }) => {
const teamId = user.team_id;

const { data: draft } = await supabase
.from("invoices")
.select("id, template")
.eq("id", id)
.single();

if (!draft) {
throw new Error("Draft not found");
}

const deliveryType =
(draft.template as InvoiceTemplate).delivery_type ?? "create";

// Update the invoice status to unpaid
const { data: invoice } = await supabase
.from("invoices")
.update({ status: "unpaid" })
.eq("id", id)
.select("*")
.single();

// Only send the email if the delivery type is create_and_send
if (deliveryType === "create_and_send") {
await tasks.trigger("send-invoice-email", {
.action(
async ({ parsedInput: { id, deliveryType }, ctx: { supabase, user } }) => {
const teamId = user.team_id;

const { data: draft } = await supabase
.from("invoices")
.select("id, template")
.eq("id", id)
.single();

if (!draft) {
throw new Error("Draft not found");
}

// Update the invoice status to unpaid
const { data: invoice } = await supabase
.from("invoices")
.update({ status: "unpaid" })
.eq("id", id)
.select("*")
.single();

// Only send the email if the delivery type is create_and_send
if (deliveryType === "create_and_send") {
await tasks.trigger("send-invoice-email", {
invoiceId: invoice?.id,
});
}

await tasks.trigger("generate-invoice", {
invoiceId: invoice?.id,
});
}

await tasks.trigger("generate-invoice", {
invoiceId: invoice?.id,
});
revalidateTag(`invoice_summary_${teamId}`);
revalidateTag(`invoices_${teamId}`);
revalidateTag(`invoice_number_${teamId}`);

revalidateTag(`invoice_summary_${teamId}`);
revalidateTag(`invoices_${teamId}`);
revalidateTag(`invoice_number_${teamId}`);

return invoice;
});
return invoice;
},
);
1 change: 1 addition & 0 deletions apps/dashboard/src/actions/invoice/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const invoiceFormSchema = z.object({

export const createInvoiceSchema = z.object({
id: z.string().uuid(),
deliveryType: z.enum(["create", "create_and_send"]),
});

export type InvoiceFormValues = z.infer<typeof invoiceFormSchema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function InvoiceSheetContent({

const { watch } = useFormContext();
const templateSize = watch("template.size");
const deliveryType = watch("template.delivery_type");

const size = templateSize === "a4" ? 650 : 740;
const isOpen = Boolean(type === "create" || type === "edit");
Expand Down Expand Up @@ -124,7 +125,7 @@ export function InvoiceSheetContent({
teamId={teamId}
customers={customers}
isSubmitting={createInvoice.isPending}
onSubmit={({ id }) => createInvoice.execute({ id })}
onSubmit={({ id }) => createInvoice.execute({ id, deliveryType })}
invoiceNumber={invoiceNumber}
/>
</SheetContent>
Expand Down

0 comments on commit 3d1fa53

Please sign in to comment.