Skip to content

Commit

Permalink
feat(CustomerCase): fetch employees by email instead of alias
Browse files Browse the repository at this point in the history
since we don't know if aliases are unique across countries
  • Loading branch information
mathiazom committed Oct 29, 2024
1 parent 926bdd1 commit b5b654d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 72 deletions.
92 changes: 46 additions & 46 deletions src/components/customerCases/customerCase/CustomerCase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Image from "next/image";

import { SanitySharedImage } from "src/components/image/SanityImage";
import Text from "src/components/text/Text";
import { fetchEmployeeByAlias } from "src/utils/employees";
import { fetchEmployeesByEmails } from "src/utils/employees";
import {
CustomerCase as CustomerCaseDocument,
CustomerCaseSection as CustomerCaseSectionObject,
Expand Down Expand Up @@ -42,14 +42,10 @@ export interface CustomerCaseProps {
export default async function CustomerCase({
customerCase,
}: CustomerCaseProps) {
const consultantsResult = await Promise.all(
customerCase.projectInfo.consultants.map((alias) =>
fetchEmployeeByAlias(alias),
),
const consultantsResult = await fetchEmployeesByEmails(
customerCase.projectInfo.consultants,
);

const consultants = consultantsResult.filter((c) => c.ok).map((c) => c.value);

return (
<div className={styles.wrapper}>
<div className={styles.content}>
Expand Down Expand Up @@ -124,14 +120,14 @@ export default async function CustomerCase({
)}
</div>
)}
{customerCase.projectInfo.consultants && (
{consultantsResult.ok && (
<div className={styles.projectInfoItem}>
<Text type={"labelRegular"}>Konsulenter</Text>
<Text
type={"labelLight"}
className={styles.projectInfoItemValue}
>
{consultants.map((c) => c.name).join(", ")}
{consultantsResult.value.map((c) => c.name).join(", ")}
</Text>
</div>
)}
Expand All @@ -142,46 +138,50 @@ export default async function CustomerCase({
<CustomerCaseSection key={section._key} section={section} />
))}
</div>
<div className={styles.consultantsWrapper}>
<Text type={"h3"}>Varianter på prosjektet</Text>
<div className={styles.consultants}>
{consultants.map(
(consultant) =>
consultant.imageThumbUrl &&
consultant.name &&
consultant.email && (
<div key={consultant.email} className={styles.consultant}>
<div className={styles.consultantImage}>
<Image
src={consultant.imageUrl ?? consultant.imageThumbUrl}
alt={consultant.name}
objectFit="cover"
fill={true}
/>
</div>
<div className={styles.consultantInfo}>
<p className={styles.consultantName}>{consultant.name}</p>
{consultant.officeName && (
<p className={styles.consultantRole}>
{consultant.officeName}
</p>
)}
{consultant.email && (
<p className={styles.consultantEmail}>
{consultant.email}
</p>
)}
{consultant.telephone && (
<p className={styles.consultantTelephone}>
{consultant.telephone}
{consultantsResult.ok && (
<div className={styles.consultantsWrapper}>
<Text type={"h3"}>Varianter på prosjektet</Text>
<div className={styles.consultants}>
{consultantsResult.value.map(
(consultant) =>
consultant.imageThumbUrl &&
consultant.name &&
consultant.email && (
<div key={consultant.email} className={styles.consultant}>
<div className={styles.consultantImage}>
<Image
src={consultant.imageUrl ?? consultant.imageThumbUrl}
alt={consultant.name}
objectFit="cover"
fill={true}
/>
</div>
<div className={styles.consultantInfo}>
<p className={styles.consultantName}>
{consultant.name}
</p>
)}
{consultant.officeName && (
<p className={styles.consultantRole}>
{consultant.officeName}
</p>
)}
{consultant.email && (
<p className={styles.consultantEmail}>
{consultant.email}
</p>
)}
{consultant.telephone && (
<p className={styles.consultantTelephone}>
{consultant.telephone}
</p>
)}
</div>
</div>
</div>
),
)}
),
)}
</div>
</div>
</div>
)}
</div>
</div>
);
Expand Down
6 changes: 0 additions & 6 deletions src/types/employees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ export function isChewbaccaEmployeesResponse(
);
}

export function isChewbaccaEmployeeResponse(
value: unknown,
): value is ChewbaccaEmployeeResponse {
return isChewbaccaEmployee(value);
}

export interface ChewbaccaEmployee {
email?: string | null;
name?: string | null;
Expand Down
33 changes: 14 additions & 19 deletions src/utils/employees.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ChewbaccaEmployee,
isChewbaccaEmployeeResponse,
isChewbaccaEmployeesResponse,
} from "src/types/employees";
import { Result, ResultError, ResultOk } from "studio/utils/result";
Expand Down Expand Up @@ -58,23 +57,19 @@ export function domainFromEmail(email: string) {
return email.split("@")[1];
}

export async function fetchEmployeeByAlias(
alias: string,
): Promise<Result<ChewbaccaEmployee, string>> {
const country = "no"; // TODO: https://github.com/varianter/chewbacca/issues/132
const employeeRes = await fetch(
new URL(`employees/${alias}?country=${country}`, CHEWBACCA_URL),
);
if (!employeeRes.ok) {
return ResultError(
`Fetch returned status ${employeeRes.status} ${employeeRes.statusText}`,
);
}
const employeeData = await employeeRes.json();
if (!isChewbaccaEmployeeResponse(employeeData)) {
return ResultError(
`Expected ChewbaccaEmployeeResponse, was ${employeeData}`,
);
export async function fetchEmployeesByEmails(
emails: string[],
): Promise<Result<ChewbaccaEmployee[], string>> {
const allEmployeesRes = await fetchAllChewbaccaEmployees();
if (!allEmployeesRes.ok) {
return allEmployeesRes;
}
return ResultOk(employeeData);
return ResultOk(
// mapping from input array (instead of filtering all employees) to preserve order
emails
.map((email) =>
allEmployeesRes.value.find((employee) => employee.email === email),
)
.filter((employee) => employee !== undefined),
);
}
2 changes: 1 addition & 1 deletion studioShared/schemas/fields/customerCaseProjectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const customerCaseProjectInfo = defineField({
// TODO: We should be able to select the consultants from a list
name: "consultants",
description:
"The consultants for the project. Use the identifier before '@' in the employee email (e.g. 'oms' for '[email protected]').",
"The consultants for the project. Use employee emails (e.g. '[email protected]').",
type: "array",
of: [{ type: "string" }],
}),
Expand Down

0 comments on commit b5b654d

Please sign in to comment.