Wrong language in emails (#1541)

* test --wip

* --wip

* --wip

* --wip

* split language into organizer-attendees

* name fix for tAttendees

* --WIP

* added attendee locale migration, --WIP

* --wip

* fixed check types --wip

* updated person language type

* test snapshot updated

* --wip

* --WIP

* --WIP

* --WIP

* test changes revert

* cleanup

* removed extra space from test

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2022-01-27 13:32:53 -07:00
1 parent 1119d7f558
commit af89de8004
26 files changed
+324 -221

No files matched your search

+19 -4
View File
@@ -43,8 +43,6 @@ const authorized = async (
const log = logger.getChildLogger({ prefix: ["[api] book:user"] });
export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
const t = await getTranslation(req.body.language ?? "en", "common");
const session = await getSession({ req: req });
if (!session?.user?.id) {
return res.status(401).json({ message: "Not authenticated" });
@@ -71,6 +69,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
name: true,
username: true,
destinationCalendar: true,
locale: true,
},
});
@@ -78,6 +77,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(404).json({ message: "User not found" });
}
const tOrganizer = await getTranslation(currentUser.locale ?? "en", "common");
if (req.method === "PATCH") {
const booking = await prisma.booking.findFirst({
where: {
@@ -112,6 +113,20 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json({ message: "booking already confirmed" });
}
const attendeesListPromises = booking.attendees.map(async (attendee) => {
return {
name: attendee.name,
email: attendee.email,
timeZone: attendee.timeZone,
language: {
translate: await getTranslation(attendee.locale ?? "en", "common"),
locale: attendee.locale ?? "en",
},
};
});
const attendeesList = await Promise.all(attendeesListPromises);
const evt: CalendarEvent = {
type: booking.title,
title: booking.title,
@@ -122,11 +137,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
email: currentUser.email,
name: currentUser.name || "Unnamed",
timeZone: currentUser.timeZone,
language: { translate: tOrganizer, locale: currentUser.locale ?? "en" },
},
attendees: booking.attendees,
attendees: attendeesList,
location: booking.location ?? "",
uid: booking.uid,
language: t,
destinationCalendar: booking?.destinationCalendar || currentUser.destinationCalendar,
};
+52 -13
View File
@@ -136,6 +136,7 @@ const userSelect = Prisma.validator<Prisma.UserArgs>()({
credentials: true,
bufferTime: true,
destinationCalendar: true,
locale: true,
},
});
@@ -152,6 +153,7 @@ const getUserNameWithBookingCounts = async (eventTypeId: number, selectedUserNam
select: {
id: true,
username: true,
locale: true,
},
});
@@ -180,8 +182,8 @@ type User = Prisma.UserGetPayload<typeof userSelect>;
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const reqBody = req.body as BookingCreateBody;
const eventTypeId = reqBody.eventTypeId;
const t = await getTranslation(reqBody.language ?? "en", "common");
const tAttendees = await getTranslation(reqBody.language ?? "en", "common");
const tGuests = await getTranslation("en", "common");
log.debug(`Booking eventType ${eventTypeId} started`);
const isTimeInPast = (time: string): boolean => {
@@ -244,6 +246,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
users.push(eventTypeUser);
}
const organizer = await prisma.user.findUnique({
where: {
id: users[0].id,
},
select: {
locale: true,
},
});
const tOrganizer = await getTranslation(organizer?.locale ?? "en", "common");
if (eventType.schedulingType === SchedulingType.ROUND_ROBIN) {
const bookingCounts = await getUserNameWithBookingCounts(
eventTypeId,
@@ -253,25 +266,41 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
users = getLuckyUsers(users, bookingCounts);
}
const invitee = [{ email: reqBody.email, name: reqBody.name, timeZone: reqBody.timeZone }];
const invitee = [
{
email: reqBody.email,
name: reqBody.name,
timeZone: reqBody.timeZone,
language: { translate: tAttendees, locale: reqBody.language ?? "en" },
},
];
const guests = (reqBody.guests || []).map((guest) => {
const g = {
email: guest,
name: "",
timeZone: reqBody.timeZone,
language: { translate: tGuests, locale: "en" },
};
return g;
});
const teamMembers =
const teamMemberPromises =
eventType.schedulingType === SchedulingType.COLLECTIVE
? users.slice(1).map((user) => ({
email: user.email || "",
name: user.name || "",
timeZone: user.timeZone,
}))
? users.slice(1).map(async function (user) {
return {
email: user.email || "",
name: user.name || "",
timeZone: user.timeZone,
language: {
translate: await getTranslation(user.locale ?? "en", "common"),
locale: user.locale ?? "en",
},
};
})
: [];
const teamMembers = await Promise.all(teamMemberPromises);
const attendeesList = [...invitee, ...guests, ...teamMembers];
const seed = `${users[0].username}:${dayjs(req.body.start).utc().format()}:${new Date().getTime()}`;
@@ -282,7 +311,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
eventType: eventType.title,
eventName: eventType.eventName,
host: users[0].name || "Nameless",
t,
t: tOrganizer,
};
const description =
@@ -294,7 +323,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const evt: CalendarEvent = {
type: eventType.title,
title: getEventName(eventNameObject),
title: getEventName(eventNameObject), //this needs to be either forced in english, or fetched for each attendee and organizer separately
description,
startTime: reqBody.start,
endTime: reqBody.end,
@@ -302,10 +331,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
name: users[0].name || "Nameless",
email: users[0].email || "Email-less",
timeZone: users[0].timeZone,
language: { translate: tOrganizer, locale: organizer?.locale ?? "en" },
},
attendees: attendeesList,
location: reqBody.location, // Will be processed by the EventManager later.
language: t,
/** For team events, we will need to handle each member destinationCalendar eventually */
destinationCalendar: eventType.destinationCalendar || users[0].destinationCalendar,
};
@@ -343,7 +372,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
attendees: {
createMany: {
data: evt.attendees,
data: evt.attendees.map((attendee) => {
//if attendee is team member, it should fetch their locale not booker's locale
//perhaps make email fetch request to see if his locale is stored, else
const retObj = {
name: attendee.name,
email: attendee.email,
timeZone: attendee.timeZone,
locale: attendee.language.locale,
};
return retObj;
}),
},
},
user: {
+19 -8
View File
@@ -89,11 +89,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
name: true,
email: true,
timeZone: true,
locale: true,
},
rejectOnNotFound: true,
});
const t = await getTranslation(req.body.language ?? "en", "common");
const attendeesListPromises = bookingToDelete.attendees.map(async (attendee) => {
return {
name: attendee.name,
email: attendee.email,
timeZone: attendee.timeZone,
language: {
translate: await getTranslation(attendee.locale ?? "en", "common"),
locale: attendee.locale ?? "en",
},
};
});
const attendeesList = await Promise.all(attendeesListPromises);
const tOrganizer = await getTranslation(organizer.locale ?? "en", "common");
const evt: CalendarEvent = {
title: bookingToDelete?.title,
@@ -105,14 +119,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
email: organizer.email,
name: organizer.name ?? "Nameless",
timeZone: organizer.timeZone,
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
},
attendees: bookingToDelete?.attendees.map((attendee) => {
const retObj = { name: attendee.name, email: attendee.email, timeZone: attendee.timeZone };
return retObj;
}),
attendees: attendeesList,
uid: bookingToDelete?.uid,
location: bookingToDelete?.location,
language: t,
destinationCalendar: bookingToDelete?.destinationCalendar || bookingToDelete?.user.destinationCalendar,
};
@@ -168,11 +179,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
email: bookingToDelete.user?.email ?? "[email protected]",
name: bookingToDelete.user?.name ?? "no user",
timeZone: bookingToDelete.user?.timeZone ?? "",
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
},
attendees: bookingToDelete.attendees,
attendees: attendeesList,
location: bookingToDelete.location ?? "",
uid: bookingToDelete.uid ?? "",
language: t,
destinationCalendar: bookingToDelete?.destinationCalendar || bookingToDelete?.user.destinationCalendar,
};
await refund(bookingToDelete, evt);
+17 -3
View File
@@ -73,7 +73,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
continue;
}
const t = await getTranslation(user.locale ?? "en", "common");
const tOrganizer = await getTranslation(user.locale ?? "en", "common");
const attendeesListPromises = booking.attendees.map(async (attendee) => {
return {
name: attendee.name,
email: attendee.email,
timeZone: attendee.timeZone,
language: {
translate: await getTranslation(attendee.locale ?? "en", "common"),
locale: attendee.locale ?? "en",
},
};
});
const attendeesList = await Promise.all(attendeesListPromises);
const evt: CalendarEvent = {
type: booking.title,
@@ -86,10 +100,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
email: user.email,
name,
timeZone: user.timeZone,
language: { translate: tOrganizer, locale: user.locale ?? "en" },
},
attendees: booking.attendees,
attendees: attendeesList,
uid: booking.uid,
language: t,
destinationCalendar: booking.destinationCalendar || user.destinationCalendar,
};