Skip to content

Commit

Permalink
Email wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Nov 14, 2023
1 parent 3c0724d commit 735a318
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 42 deletions.
71 changes: 49 additions & 22 deletions apps/dashboard/src/jobs/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { client } from "@/trigger";
import { TransactionsEmail } from "@midday/email/emails/transactions";
import { getTransactions } from "@midday/gocardless";
import { TriggerEvents, trigger, triggerBulk } from "@midday/notification";
import { TriggerEvents, triggerBulk } from "@midday/notification";
import { Database } from "@midday/supabase/src/types";
import { renderAsync } from "@react-email/components";
import { eventTrigger } from "@trigger.dev/sdk";
import { Supabase, SupabaseManagement } from "@trigger.dev/supabase";
import { capitalCase } from "change-case";
Expand Down Expand Up @@ -139,13 +141,23 @@ client.defineJob({
)
.select();

const { data: usersData } = await io.supabase.client
.from("users_on_team")
.select("team_id, user:user_id(id, full_name, avatar_url, email, locale)")
.eq("team_id", data?.team_id);

if (transactionsData?.length && transactionsData.length > 0) {
revalidateTag(`transactions_${data?.team_id}`);
revalidateTag(`spending_${data?.team_id}`);
revalidateTag(`metrics_${data?.team_id}`);

// Send notification for each transaction
// triggerBulk(
// transactionsData.map((transaction) => ({
// name: TriggerEvents.TransactionNewInApp,
// payload: {
// html: "TODO",
// description: "You have a new transaction of -1000 from Github",
// currency: "SEK",
// },
// users: [
// {
Expand All @@ -159,27 +171,42 @@ client.defineJob({
// }))
// );

// Send email with react-email-template
// trigger({
// name: TriggerEvents.TransactionNewEmail,
// payload: {
// subject: "New transactions",
// html: "TODO",
// },
// users: [
// {
// subscriberId: "",
// teamId: "123",
// email: "",
// fullName: "Pontus Abrahamsson",
// avatarUrl: "https://",
// },
// ],
// });
const emailEvents = await Promise.all(
usersData?.map(async ({ user, team_id }) => {
const html = await renderAsync(
TransactionsEmail({
fullName: user.full_name,
transactions: transactionsData.map((transaction) => ({
id: transaction.id,
date: transaction.date,
amount: transaction.amount,
name: transaction.name,
currency: transaction.currency,
})),
locale: user.locale,
})
);

revalidateTag(`transactions_${data?.team_id}`);
revalidateTag(`spending_${data?.team_id}`);
revalidateTag(`metrics_${data?.team_id}`);
return {
name: TriggerEvents.TransactionNewEmail,
payload: {
subject: "New transactions",
html,
},
user: {
subscriberId: user.id,
teamId: team_id,
email: user.email,
fullName: user.full_name,
avatarUrl: user.avatar_url,
},
};
})
);

if (emailEvents?.length) {
triggerBulk(emailEvents);
}
}

if (error) {
Expand Down
Binary file modified bun.lockb
Binary file not shown.
14 changes: 6 additions & 8 deletions packages/email/emails/transactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { cn } from "@midday/ui/utils";
import {
Body,
Button,
Column,
Container,
Font,
Head,
Expand All @@ -12,7 +11,6 @@ import {
Img,
Link,
Preview,
Row,
Section,
Tailwind,
Text,
Expand All @@ -29,7 +27,7 @@ type Transaction = {
};

interface TransactionsEmailEmailProps {
firstName: string;
fullName: string;
transactions: Transaction[];
locale: string;
}
Expand Down Expand Up @@ -76,10 +74,12 @@ const baseAppUrl =
: "http://localhost:3001";

export const TransactionsEmail = ({
firstName = "Viktor",
fullName = "Viktor Hofte",
transactions = defaultTransactions,
locale = "en",
}: TransactionsEmailEmailProps) => {
const firstName = fullName.split(" ").at(0);

const previewText = `Hi ${firstName}, We found 5 transactions thats missing receipts. Feel free to attach them to ease your own or your accountants work for upcoming declerations.`;

return (
Expand Down Expand Up @@ -150,7 +150,7 @@ export const TransactionsEmail = ({
</thead>

<tbody>
{transactions.map((transaction) => (
{transactions?.map((transaction) => (
<tr key={transaction.id}>
<td align="left">
<Text className="text-[14px] m-0 p-0 mt-1 pb-1">
Expand Down Expand Up @@ -190,11 +190,9 @@ export const TransactionsEmail = ({

<Section className="text-center mt-[32px] mb-[32px]">
<Button
pX={20}
pY={12}
className="bg-[#000000] rounded-xl text-white text-[12px] font-semibold no-underline text-center"
href={`${baseAppUrl}/transactions?from_id=${
transactions.at(0).id
transactions.at(0)?.id
}`}
>
View transactions
Expand Down
4 changes: 2 additions & 2 deletions packages/email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"export": "email export"
},
"dependencies": {
"@react-email/components": "0.0.6",
"@react-email/components": "0.0.11",
"date-fns": "^2.30.0",
"react-email": "1.9.3"
"react-email": "1.9.5"
},
"devDependencies": {
"typescript": "^5.2.2"
Expand Down
18 changes: 9 additions & 9 deletions packages/notification/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ type TriggerUser = {
type TriggerPayload = {
name: TriggerEvents;
payload: any;
users: TriggerUser[];
user: TriggerUser;
tenant?: string; // NOTE: Currently no way to listen for messages with tenant, we use team_id + user_id for unique
};

export async function trigger(data: TriggerPayload) {
return novu.trigger(data.name, {
to: data.users.map((user) => ({
...user,
to: {
...data.user,
// Prefix subscriber id with team id
subscriberId: `${user.teamId}_${user.subscriberId}`,
})),
subscriberId: `${data.user.teamId}_${data.user.subscriberId}`,
},
payload: data.payload,
tenant: data.tenant,
});
Expand All @@ -39,11 +39,11 @@ export async function triggerBulk(events: TriggerPayload[]) {
return novu.bulkTrigger(
events.map((data) => ({
name: data.name,
to: data.users.map((user) => ({
...user,
to: {
...data.user,
// Prefix subscriber id with team id
subscriberId: `${user.teamId}_${user.subscriberId}`,
})),
subscriberId: `${data.user.teamId}_${data.user.subscriberId}`,
},
payload: data.payload,
tenant: data.tenant,
}))
Expand Down
1 change: 0 additions & 1 deletion packages/supabase/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ export async function getTransaction(supabase: Client, id: string) {
)
.eq("id", id)
.single()

.throwOnError();
}

Expand Down

0 comments on commit 735a318

Please sign in to comment.