Skip to content

Commit

Permalink
chore: definition du model Organisation via zod (#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
moroine authored Feb 23, 2024
1 parent 2a0c187 commit 36be481
Show file tree
Hide file tree
Showing 15 changed files with 592 additions and 719 deletions.
4 changes: 2 additions & 2 deletions server/src/common/actions/helpers/permissions-organisme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PermissionsOrganisme,
assertUnreachable,
} from "shared";
import { Organisation } from "shared/models/data/organisations.model";
import { IOrganisation } from "shared/models/data/organisations.model";
import { IOrganisme } from "shared/models/data/organismes.model";

import { getOrganismeById } from "@/common/actions/organismes/organismes.actions";
Expand All @@ -21,7 +21,7 @@ import { findOrganismeFormateursIds } from "./permissions";

export type OrganismeWithPermissions = IOrganisme & { permissions: PermissionsOrganisme };

export async function getAcl(organisation: Organisation): Promise<Acl> {
export async function getAcl(organisation: IOrganisation): Promise<Acl> {
switch (organisation.type) {
case "ORGANISME_FORMATION": {
const userOrganisme = await organismesDb().findOne({
Expand Down
4 changes: 2 additions & 2 deletions server/src/common/actions/helpers/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectId } from "mongodb";
import { OrganisationOrganismeFormation } from "shared/models/data/organisations.model";
import { IOrganisationOrganismeFormation } from "shared/models/data/organisations.model";
import { IOrganisme } from "shared/models/data/organismes.model";

import { getOrganismeById } from "@/common/actions/organismes/organismes.actions";
Expand All @@ -10,7 +10,7 @@ import { organismesDb } from "@/common/model/collections";
* Liste tous les organismes accessibles pour une organisation (dont l'organisme lié à l'organisation)
*/
export async function findOrganismesAccessiblesByOrganisationOF(
organisation: OrganisationOrganismeFormation
organisation: IOrganisationOrganismeFormation
): Promise<ObjectId[]> {
const userOrganisme = await organismesDb().findOne({
siret: organisation.siret,
Expand Down
9 changes: 5 additions & 4 deletions server/src/common/actions/organisations.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { format } from "date-fns";
import { ObjectId, WithId } from "mongodb";
import { REGIONS_BY_CODE, DEPARTEMENTS_BY_CODE, ACADEMIES_BY_CODE } from "shared";
import { IInvitation } from "shared/models/data/invitations.model";
import { NewOrganisation, Organisation } from "shared/models/data/organisations.model";
import { IOrganisationCreate, IOrganisation } from "shared/models/data/organisations.model";
import { IUsersMigration } from "shared/models/data/usersMigration.model";

import logger from "@/common/logger";
Expand All @@ -17,16 +17,17 @@ import { OrganismeWithPermissions } from "./helpers/permissions-organisme";
import { getOrganismeProjection } from "./organismes/organismes.actions";
import { getUserById } from "./users.actions";

export async function createOrganisation(organisation: NewOrganisation): Promise<ObjectId> {
export async function createOrganisation(organisation: IOrganisationCreate): Promise<ObjectId> {
const { insertedId } = await organisationsDb().insertOne({
_id: new ObjectId(),
created_at: getCurrentTime(),
...organisation,
});
return insertedId;
}

export async function getOrganisationById(organisationId: ObjectId): Promise<Organisation> {
const organisation = await organisationsDb().findOne<Organisation>({ _id: organisationId });
export async function getOrganisationById(organisationId: ObjectId): Promise<IOrganisation> {
const organisation = await organisationsDb().findOne<IOrganisation>({ _id: organisationId });
if (!organisation) {
throw Boom.notFound(`missing organisation ${organisationId}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import escapeStringRegexp from "escape-string-regexp";
import { OrganismeSupportInfo } from "shared/models";
import { OffreFormation } from "shared/models/data/@types/OffreFormation";
import { IFormationCatalogue } from "shared/models/data/formationsCatalogue.model";
import { OrganisationOrganismeFormation } from "shared/models/data/organisations.model";
import { IOrganisationOrganismeFormation } from "shared/models/data/organisations.model";
import { IUsersMigration } from "shared/models/data/usersMigration.model";

import { getEtablissement } from "@/common/apis/ApiEntreprise";
Expand Down Expand Up @@ -222,7 +222,7 @@ export async function findOrganismesSupportInfoBySiret(siret: string): Promise<O
getOffreFormations(siret),
fiabilisationUaiSiretDb().find({ siret }).toArray(),
organisationsDb()
.aggregate<OrganisationOrganismeFormation & { users: IUsersMigration[] }>([
.aggregate<IOrganisationOrganismeFormation & { users: IUsersMigration[] }>([
{ $match: { type: "ORGANISME_FORMATION", siret } },
{
$lookup: {
Expand Down
4 changes: 2 additions & 2 deletions server/src/common/model/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import invitationsModelDescriptor, { IInvitation } from "shared/models/data/invi
import jobEventsModelDescriptor from "shared/models/data/jobEvents.model";
import JwtSessionsModelDescriptor, { IJwtSession } from "shared/models/data/jwtSessions.model";
import MaintenanceMessagesModelDescriptor, { IMaintenanceMessage } from "shared/models/data/maintenanceMessages.model";
import organisationsModelDescriptor, { Organisation } from "shared/models/data/organisations.model";
import organisationsModelDescriptor, { IOrganisation } from "shared/models/data/organisations.model";
import OrganismesModelDescriptor, { IOrganisme } from "shared/models/data/organismes.model";
import OrganismesReferentielModelDescriptor, {
IOrganismeReferentiel,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const usersMigrationDb = () => getDbCollection<IUsersMigration>(usersMigr
export const jwtSessionsDb = () => getDbCollection<IJwtSession>(JwtSessionsModelDescriptor.collectionName);
export const organismesDb = () => getDbCollection<IOrganisme>(OrganismesModelDescriptor.collectionName);
export const invitationsDb = () => getDbCollection<IInvitation>(invitationsModelDescriptor.collectionName);
export const organisationsDb = () => getDbCollection<Organisation>(organisationsModelDescriptor.collectionName);
export const organisationsDb = () => getDbCollection<IOrganisation>(organisationsModelDescriptor.collectionName);
export const organismesReferentielDb = () =>
getDbCollection<IOrganismeReferentiel>(OrganismesReferentielModelDescriptor.collectionName);
export const maintenanceMessageDb = () =>
Expand Down
6 changes: 3 additions & 3 deletions server/src/common/model/internal/AuthContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ObjectId } from "mongodb";
import { Acl } from "shared";
import { Organisation } from "shared/models/data/organisations.model";
import { IOrganisation } from "shared/models/data/organisations.model";

export interface AuthContext<IOrganisation = Organisation> {
export interface AuthContext<I = IOrganisation> {
_id: ObjectId;
civility: string;
nom: string;
Expand All @@ -14,7 +14,7 @@ export interface AuthContext<IOrganisation = Organisation> {
invalided_token?: boolean;

// populated via $lookup
organisation: IOrganisation;
organisation: I;

// field used for ERPs
source?: string;
Expand Down
Loading

0 comments on commit 36be481

Please sign in to comment.