fix: Implement new avatars in insights (#13996)

* fix: Implement new avatars in insights

* fix: Exposed type errors, previously hidden by any,any type

---------

Co-authored-by: Omar López <[email protected]>
This commit is contained in:
Alex van AndelandOmar López authored and GitHub committed 2024-03-07 17:00:11 +00:00
1 parent 286f696302
commit e60454750e
2 files changed
+61 -34

No files matched your search

@@ -7,7 +7,13 @@ export const TotalBookingUsersTable = ({
data,
}: {
data:
| { userId: number | null; user: User; emailMd5?: string; count: number; Username?: string }[]
| {
userId: number | null;
user: Pick<User, "avatarUrl" | "name">;
emailMd5?: string;
count: number;
username?: string;
}[]
| undefined;
}) => {
return (
@@ -21,7 +27,7 @@ export const TotalBookingUsersTable = ({
<Avatar
alt={item.user.name || ""}
size="sm"
imageSrc={`/${item.user.username}/avatar.png`}
imageSrc={item.user.avatarUrl}
title={item.user.name || ""}
className="m-2"
/>
@@ -17,6 +17,22 @@ const UserBelongsToTeamInput = z.object({
isAll: z.boolean().optional(),
});
const buildHashMapForUsers = <
T extends { avatarUrl: string | null; id: number; username: string | null; [key: string]: unknown }
>(
usersFromTeam: T[]
) => {
const userHashMap = new Map<number | null, Omit<T, "avatarUrl"> & { avatarUrl: string }>();
usersFromTeam.forEach((user) => {
userHashMap.set(user.id, {
...user,
// TODO: Use AVATAR_FALLBACK when avatar.png endpoint is fased out
avatarUrl: user.avatarUrl || `/${user.username}/avatar.png`,
});
});
return userHashMap;
};
const userBelongsToTeamProcedure = authedProcedure.use(async ({ ctx, next, getRawInput }) => {
const parse = UserBelongsToTeamInput.safeParse(await getRawInput());
if (!parse.success) {
@@ -70,11 +86,12 @@ const userBelongsToTeamProcedure = authedProcedure.use(async ({ ctx, next, getRa
});
});
const UserSelect = {
const userSelect = {
id: true,
name: true,
email: true,
username: true,
avatarUrl: true,
};
const emptyResponseEventsByStatus = {
@@ -969,9 +986,13 @@ export const insightsRouter = router({
take: 10,
});
const userIds = bookingsFromTeam
.filter((booking) => typeof booking.userId === "number")
.map((booking) => booking.userId);
const userIds = bookingsFromTeam.reduce((userIds: number[], booking) => {
if (typeof booking.userId === "number" && !userIds.includes(booking.userId)) {
userIds.push(booking.userId);
}
return userIds;
}, []);
if (userIds.length === 0) {
return [];
}
@@ -979,21 +1000,20 @@ export const insightsRouter = router({
const usersFromTeam = await ctx.insightsDb.user.findMany({
where: {
id: {
in: userIds as number[],
in: userIds,
},
},
select: UserSelect,
select: userSelect,
});
const userHashMap = new Map();
usersFromTeam.forEach((user) => {
userHashMap.set(user.id, user);
});
const userHashMap = buildHashMapForUsers(usersFromTeam);
const result = bookingsFromTeam.map((booking) => {
return {
userId: booking.userId,
user: userHashMap.get(booking.userId),
// We know with 100% certainty that userHashMap.get(...) will retrieve a user
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
user: userHashMap.get(booking.userId)!,
emailMd5: md5(user?.email),
count: booking._count.id,
};
@@ -1102,34 +1122,35 @@ export const insightsRouter = router({
take: 10,
});
const userIds = bookingsFromTeam
.filter((booking) => typeof booking.userId === "number")
.map((booking) => booking.userId);
const userIds = bookingsFromTeam.reduce((userIds: number[], booking) => {
if (typeof booking.userId === "number" && !userIds.includes(booking.userId)) {
userIds.push(booking.userId);
}
return userIds;
}, []);
if (userIds.length === 0) {
return [];
}
const usersFromTeam = await ctx.insightsDb.user.findMany({
where: {
id: {
in: userIds as number[],
in: userIds,
},
},
select: UserSelect,
select: userSelect,
});
const userHashMap = new Map();
usersFromTeam.forEach((user) => {
userHashMap.set(user.id, user);
});
const userHashMap = buildHashMapForUsers(usersFromTeam);
const result = bookingsFromTeam.map((booking) => {
return {
userId: booking.userId,
user: userHashMap.get(booking.userId),
emailMd5: md5(user?.email),
count: booking._count.id,
};
});
const result = bookingsFromTeam.map((booking) => ({
userId: booking.userId,
// We know with 100% certainty that userHashMap.get(...) will retrieve a user
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
user: userHashMap.get(booking.userId)!,
emailMd5: md5(user?.email),
count: booking._count.id,
}));
return result;
}),
@@ -1141,7 +1162,7 @@ export const insightsRouter = router({
where: {
id: user.id,
},
select: UserSelect,
select: userSelect,
});
if (!userData) {
@@ -1262,7 +1283,7 @@ export const insightsRouter = router({
},
include: {
user: {
select: UserSelect,
select: userSelect,
},
},
distinct: ["userId"],
@@ -1278,7 +1299,7 @@ export const insightsRouter = router({
},
include: {
user: {
select: UserSelect,
select: userSelect,
},
},
});
@@ -1298,7 +1319,7 @@ export const insightsRouter = router({
},
include: {
user: {
select: UserSelect,
select: userSelect,
},
},
distinct: ["userId"],