From bbed40820182e7d429c6fbd37ae0839a3324e4ad Mon Sep 17 00:00:00 2001 From: Alexandre Garbe Date: Fri, 12 Jul 2024 11:58:07 +0200 Subject: [PATCH] feat(api+admin-react): updated organism -> account resolver in order to return multiple user accounts --- .../[organismId]/manager/agencyManagerPage.hook.ts | 4 ++-- .../src/app/(aap)/agencies-settings/layout.tsx | 8 ++++---- .../features/getAccountByOrganismIdAndEmail.ts | 12 ++++++++++++ .../organism/features/getAccountByOrganismId.ts | 10 ---------- .../organism/features/getAccountsByOrganismId.ts | 12 ++++++++++++ .../organism/features/updateOrganismAccount.ts | 7 +++++-- packages/reva-api/modules/organism/organism.graphql | 2 +- .../reva-api/modules/organism/organism.resolvers.ts | 6 +++--- 8 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 packages/reva-api/modules/account/features/getAccountByOrganismIdAndEmail.ts delete mode 100644 packages/reva-api/modules/organism/features/getAccountByOrganismId.ts create mode 100644 packages/reva-api/modules/organism/features/getAccountsByOrganismId.ts diff --git a/packages/reva-admin-react/src/app/(aap)/agencies-settings/[organismId]/manager/agencyManagerPage.hook.ts b/packages/reva-admin-react/src/app/(aap)/agencies-settings/[organismId]/manager/agencyManagerPage.hook.ts index 49fcf8e45..ef731c758 100644 --- a/packages/reva-admin-react/src/app/(aap)/agencies-settings/[organismId]/manager/agencyManagerPage.hook.ts +++ b/packages/reva-admin-react/src/app/(aap)/agencies-settings/[organismId]/manager/agencyManagerPage.hook.ts @@ -8,7 +8,7 @@ const organismQuery = graphql(` query getOrganismForAgencyManagerPage($organismId: ID!) { organism_getOrganism(id: $organismId) { id - organismOnAccount { + accounts { id firstname lastname @@ -49,7 +49,7 @@ export const useAgencyManagerPage = () => { }), }); - const account = organismResponse?.organism_getOrganism?.organismOnAccount; + const account = organismResponse?.organism_getOrganism?.accounts?.[0]; return { account, diff --git a/packages/reva-admin-react/src/app/(aap)/agencies-settings/layout.tsx b/packages/reva-admin-react/src/app/(aap)/agencies-settings/layout.tsx index 0a71d217c..5472cbb19 100644 --- a/packages/reva-admin-react/src/app/(aap)/agencies-settings/layout.tsx +++ b/packages/reva-admin-react/src/app/(aap)/agencies-settings/layout.tsx @@ -23,7 +23,7 @@ const agenciesInfoForConnectedUserQuery = graphql(` informationsCommerciales { nom } - organismOnAccount { + accounts { id email firstname @@ -181,10 +181,10 @@ const AgenciesSettingsLayout = ({ children }: { children: ReactNode }) => { }, items: [ ...agencies - .map((a) => a.organismOnAccount) - .map((o) => { + .flatMap((agency) => agency.accounts) + .map((a) => { return { - text: `${o?.firstname} ${o?.lastname}`, + text: `${a?.firstname} ${a?.lastname}`, linkProps: { href: "#", }, diff --git a/packages/reva-api/modules/account/features/getAccountByOrganismIdAndEmail.ts b/packages/reva-api/modules/account/features/getAccountByOrganismIdAndEmail.ts new file mode 100644 index 000000000..ecd043574 --- /dev/null +++ b/packages/reva-api/modules/account/features/getAccountByOrganismIdAndEmail.ts @@ -0,0 +1,12 @@ +import { prismaClient } from "../../../prisma/client"; + +export const getAccountsByOrganismIdAndEmail = ({ + organismId, + email, +}: { + organismId: string; + email: string; +}) => + prismaClient.account.findUnique({ + where: { organismId, email }, + }); diff --git a/packages/reva-api/modules/organism/features/getAccountByOrganismId.ts b/packages/reva-api/modules/organism/features/getAccountByOrganismId.ts deleted file mode 100644 index a8aefc9bb..000000000 --- a/packages/reva-api/modules/organism/features/getAccountByOrganismId.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { prismaClient } from "../../../prisma/client"; - -export const getAccountByOrganismId = ({ - organismId, -}: { - organismId: string; -}) => - prismaClient.account.findFirst({ - where: { organismId }, - }); diff --git a/packages/reva-api/modules/organism/features/getAccountsByOrganismId.ts b/packages/reva-api/modules/organism/features/getAccountsByOrganismId.ts new file mode 100644 index 000000000..dc852ea24 --- /dev/null +++ b/packages/reva-api/modules/organism/features/getAccountsByOrganismId.ts @@ -0,0 +1,12 @@ +import { prismaClient } from "../../../prisma/client"; + +export const getAccountsByOrganismId = async ({ + organismId, +}: { + organismId: string; +}) => + prismaClient.organism + .findUnique({ + where: { id: organismId }, + }) + .accounts(); diff --git a/packages/reva-api/modules/organism/features/updateOrganismAccount.ts b/packages/reva-api/modules/organism/features/updateOrganismAccount.ts index 39818a971..7300fbb05 100644 --- a/packages/reva-api/modules/organism/features/updateOrganismAccount.ts +++ b/packages/reva-api/modules/organism/features/updateOrganismAccount.ts @@ -1,13 +1,16 @@ +import { getAccountsByOrganismIdAndEmail } from "../../account/features/getAccountByOrganismIdAndEmail"; import { updateAccountById } from "../../account/features/updateAccount"; import { UpdateOrganismAccountInput } from "../organism.types"; -import { getAccountByOrganismId } from "./getAccountByOrganismId"; export const updateOrganismAccount = async ({ params: { organismId, accountEmail, accountFirstname, accountLastname }, }: { params: UpdateOrganismAccountInput; }) => { - const account = await getAccountByOrganismId({ organismId }); + const account = await getAccountsByOrganismIdAndEmail({ + organismId, + email: accountEmail, + }); if (!account) { throw Error("Compte utilisateur non trouvé"); diff --git a/packages/reva-api/modules/organism/organism.graphql b/packages/reva-api/modules/organism/organism.graphql index b6b693738..805e0e261 100644 --- a/packages/reva-api/modules/organism/organism.graphql +++ b/packages/reva-api/modules/organism/organism.graphql @@ -35,7 +35,7 @@ type Organism { informationsCommerciales: OrganismInformationsCommerciales maisonMereAAP: MaisonMereAAP managedDegrees: [OrganismOnDegree!]! - organismOnAccount: Account + accounts: [Account!]! domaines: [Domaine!]! conventionCollectives: [ConventionCollective!]! llToEarth: String diff --git a/packages/reva-api/modules/organism/organism.resolvers.ts b/packages/reva-api/modules/organism/organism.resolvers.ts index 6e020fa17..993c6dae6 100644 --- a/packages/reva-api/modules/organism/organism.resolvers.ts +++ b/packages/reva-api/modules/organism/organism.resolvers.ts @@ -24,7 +24,7 @@ import { createOrganismWithMaisonMereAAP } from "./features/createOrganismWithMa import { createOrUpdateInformationsCommerciales } from "./features/createOrUpdateInformationsCommerciales"; import { updateOrganismDegreesAndDomaines } from "./features/updateOrganismDegreesAndDomaines"; import { findOrganismOnDegreeByOrganismId } from "./features/findOrganismOnDegreeByOrganismId"; -import { getAccountByOrganismId } from "./features/getAccountByOrganismId"; +import { getAccountsByOrganismId } from "./features/getAccountsByOrganismId"; import { getAgencesByGestionnaireAccountId } from "./features/getAgencesByGestionnaireAccountId"; import { getLLToEarthFromZip } from "./features/getLLToEarthFromZip"; import { getMaisonMereAAPByGestionnaireAccountId } from "./features/getMaisonMereAAPByGestionnaireAccountId"; @@ -91,8 +91,8 @@ const unsafeResolvers = { }, managedDegrees: (organism: Organism) => findOrganismOnDegreeByOrganismId({ organismId: organism.id }), - organismOnAccount: ({ id: organismId }: Organism) => - getAccountByOrganismId({ + accounts: ({ id: organismId }: Organism) => + getAccountsByOrganismId({ organismId, }), domaines: ({ id: organismId }: Organism) =>