Skip to content

Commit

Permalink
fix(moderation): add nickname to new conversatino
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil committed Aug 29, 2024
1 parent b0813e1 commit a5337f4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/~/app/layout/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export function Main_Layout({ children }: PropsWithChildren) {
);
}

/**
* @todo(dougalsduteil) Move this to another package
*/
export function userinfo_to_username(userinfo: {
given_name: string;
usual_name: string;
Expand Down
7 changes: 6 additions & 1 deletion packages/~/infra/crisp/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export async function get_user(config: Config, { email }: { email: string }) {

export async function create_conversation(
config: Config,
{ email, subject }: Pick<ConversationMeta, "email" | "subject">,
{
email,
nickname,
subject,
}: Pick<ConversationMeta, "email" | "nickname" | "subject">,
) {
const { session_id } = await fetch_crisp<CreateConversationRoute>(config, {
endpoint: `/v1/website/${config.website_id}/conversation`,
Expand All @@ -98,6 +102,7 @@ export async function create_conversation(
searchParams: {},
body: {
email,
nickname,
segments: ["email", "moderation"],
subject,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//

import { NotFoundError } from "@~/app.core/error";
import { userinfo_to_username } from "@~/app.layout";
import { create_conversation } from "@~/crisp.lib";
import { update_moderation_by_id } from "@~/moderations.repository/update_moderation_by_id";
import type {
Expand All @@ -15,15 +17,31 @@ export async function create_and_send_email_to_user(
{ message, subject, to }: RejectedFullMessage,
) {
const { crisp_config, moderation, pg } = context;
const user = await pg.query.users.findFirst({
columns: { given_name: true, family_name: true },
where: (table, { eq }) => eq(table.id, moderation.user_id),
});
if (!user) throw new NotFoundError(`User not found`);
const nickname = userinfo_to_username({
given_name: user.given_name ?? "",
usual_name: user.family_name ?? "",
});
const { session_id } = await create_conversation(crisp_config, {
email: to,
subject,
nickname,
});

await respond_to_ticket(context, { message, subject, to: session_id });

await update_moderation_by_id(pg, {
moderation_id: moderation.id,
ticket_id: session_id,
});

await respond_to_ticket(
{
...context,
moderation: { ...context.moderation, ticket_id: session_id },
},
{ message, subject, to: session_id },
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function respond_to_ticket(
) {
return match(context.moderation.ticket_id)
.with(null, () => {
throw new NotFoundError("Ticket not found.");
throw new NotFoundError("No existing ticket.");
})
.when(is_crisp_ticket, () => respond_in_conversation(context, full_message))
.when(is_zammad_ticket, () =>
Expand Down

0 comments on commit a5337f4

Please sign in to comment.