The admin comment action walks three entity paths to find the client who needs the email
When I post a comment in the admin panel, the server action receives three values — a body, an entityType, and an entityId. The entityType is one of session, milestone, or goal. The comment needs to be created on the right entity and the right client needs to be notified by email. The create call uses spread operators to set exactly one foreign key — if entityType is session, the data includes sessionId set to the entityId. If milestone, milestoneId. If goal, goalId. The other two foreign keys are omitted entirely because spread on an empty object adds nothing. Three conditional spreads, one create call, and the comment attaches to the correct entity without any conditional branching around the Prisma query itself. The notification lookup is the interesting part. Comments do not have a direct relationship to clients. A session comment reaches the client through session, then project, then client. A milestone comment goes through milestone, then project, then client. A goal comment goes through goal, then project, then client. The action runs three if-blocks — one per entity type — each executing a Prisma findUnique with a nested include chain that traverses from the entity to its project to the project's client. All three chains select the same three fields — email, name, and clerkId. All three chains assign the result to the same two variables — client and projectName. After the branching, a single notification call fires if the client has a clerkId, meaning they have actually signed up and can receive emails. The notification itself is fire-and-forget — the catch callback is an empty function. If Resend is down, the comment still exists in the database. The client will see it when they next visit the portal. The authorName is hardcoded to James and isJames is hardcoded to true because only I can post comments through the admin panel. The client-side addComment action in the portal sets isJames to false and reads the client's name from the database. Two comment paths, one visual distinction, three notification chains, and the email always finds its way to the right inbox.
Comments coming soon
Sign in with TikTok to leave a comment. Coming soon.