-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract force join organization
- Loading branch information
1 parent
0b1017d
commit c6a6098
Showing
24 changed files
with
289 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class NotFoundError extends Error {} |
65 changes: 65 additions & 0 deletions
65
packages/identite/src/managers/organization/force-join-organization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { NotFoundError } from "#src/errors"; | ||
import type { Organization, User } from "#src/types"; | ||
import * as chai from "chai"; | ||
import "chai-as-promised"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
import { describe } from "mocha"; | ||
import { forceJoinOrganizationFactory } from "./force-join-organization.js"; | ||
chai.use(chaiAsPromised); | ||
const expect = chai.expect; | ||
|
||
describe(forceJoinOrganizationFactory.name, () => { | ||
it("should update the organization user link ", async () => { | ||
const forceJoinOrganization = forceJoinOrganizationFactory({ | ||
findById: () => Promise.resolve({ id: 42 } as Organization), | ||
findEmailDomainsByOrganizationId: () => Promise.resolve([]), | ||
findUserById: () => | ||
Promise.resolve({ email: "[email protected]" } as User), | ||
linkUserToOrganization: (values) => Promise.resolve(values as any), | ||
}); | ||
|
||
await expect( | ||
forceJoinOrganization({ | ||
organization_id: 42, | ||
user_id: 42, | ||
}), | ||
).eventually.deep.equal({ | ||
is_external: false, | ||
organization_id: 42, | ||
user_id: 42, | ||
verification_type: "no_validation_means_available", | ||
}); | ||
}); | ||
|
||
it("❎ throws NotFoundError for unknown organization", async () => { | ||
const forceJoinOrganization = forceJoinOrganizationFactory({ | ||
findById: () => Promise.resolve(undefined), | ||
findEmailDomainsByOrganizationId: () => Promise.resolve([]), | ||
findUserById: () => Promise.resolve({ id: 42 } as User), | ||
linkUserToOrganization: () => Promise.reject(), | ||
}); | ||
|
||
await expect( | ||
forceJoinOrganization({ | ||
organization_id: 42, | ||
user_id: 42, | ||
}), | ||
).rejectedWith(NotFoundError); | ||
}); | ||
|
||
it("❎ throws NotFoundError for unknown user", async () => { | ||
const forceJoinOrganization = forceJoinOrganizationFactory({ | ||
findById: () => Promise.resolve({ id: 42 } as Organization), | ||
findEmailDomainsByOrganizationId: () => Promise.resolve([]), | ||
findUserById: () => Promise.resolve(undefined), | ||
linkUserToOrganization: () => Promise.reject(), | ||
}); | ||
|
||
await expect( | ||
forceJoinOrganization({ | ||
organization_id: 42, | ||
user_id: 42, | ||
}), | ||
).rejectedWith(NotFoundError); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
packages/identite/src/managers/organization/force-join-organization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
|
||
import { NotFoundError } from "#src/errors"; | ||
import type { FindEmailDomainsByOrganizationIdHandler } from "#src/repositories/email-domain"; | ||
import type { | ||
FindByIdHandler as FindOrganizationByIdHandler, | ||
LinkUserToOrganizationHandler, | ||
} from "#src/repositories/organization"; | ||
import type { FindByIdHandler as FindUserByIdHandler } from "#src/repositories/user"; | ||
import type { BaseUserOrganizationLink } from "#src/types"; | ||
import { getEmailDomain } from "@gouvfr-lasuite/proconnect.core/services/email"; | ||
import { isEmpty, some } from "lodash-es"; | ||
|
||
// | ||
|
||
type FactoryDependencies = { | ||
findById: FindOrganizationByIdHandler; | ||
findEmailDomainsByOrganizationId: FindEmailDomainsByOrganizationIdHandler; | ||
findUserById: FindUserByIdHandler; | ||
linkUserToOrganization: LinkUserToOrganizationHandler; | ||
}; | ||
|
||
// | ||
|
||
export function forceJoinOrganizationFactory({ | ||
findById, | ||
findEmailDomainsByOrganizationId, | ||
findUserById, | ||
linkUserToOrganization, | ||
}: FactoryDependencies) { | ||
return async function forceJoinOrganization({ | ||
organization_id, | ||
user_id, | ||
is_external = false, | ||
}: { | ||
organization_id: number; | ||
user_id: number; | ||
is_external?: boolean; | ||
}) { | ||
const user = await findUserById(user_id); | ||
const organization = await findById(organization_id); | ||
if (isEmpty(user) || isEmpty(organization)) { | ||
throw new NotFoundError(); | ||
} | ||
const { email } = user; | ||
const domain = getEmailDomain(email); | ||
const organizationEmailDomains = | ||
await findEmailDomainsByOrganizationId(organization_id); | ||
|
||
let link_verification_type: BaseUserOrganizationLink["verification_type"]; | ||
if ( | ||
some(organizationEmailDomains, { | ||
domain, | ||
verification_type: "verified", | ||
}) || | ||
some(organizationEmailDomains, { | ||
domain, | ||
verification_type: "trackdechets_postal_mail", | ||
}) || | ||
some(organizationEmailDomains, { domain, verification_type: "external" }) | ||
) { | ||
link_verification_type = "domain"; | ||
} else { | ||
link_verification_type = "no_validation_means_available"; | ||
} | ||
|
||
return await linkUserToOrganization({ | ||
organization_id, | ||
user_id, | ||
is_external, | ||
verification_type: link_verification_type, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// | ||
|
||
export * from "./force-join-organization.js"; | ||
export * from "./get-organization-info.js"; | ||
export * from "./mark-domain-as-verified.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/identite/src/repositories/organization/link-user-to-organization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { emptyDatabase, migrate, pg } from "#testing"; | ||
import FakeTimers from "@sinonjs/fake-timers"; | ||
import * as chai from "chai"; | ||
import "chai-as-promised"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
import { describe } from "mocha"; | ||
import { linkUserToOrganizationFactory } from "./link-user-to-organization.js"; | ||
chai.use(chaiAsPromised); | ||
const expect = chai.expect; | ||
|
||
// | ||
|
||
const linkUserToOrganization = linkUserToOrganizationFactory({ pg: pg as any }); | ||
|
||
describe(linkUserToOrganizationFactory.name, () => { | ||
before(migrate); | ||
beforeEach(emptyDatabase); | ||
|
||
it("should link user to organization", async () => { | ||
await pg.sql` | ||
INSERT INTO organizations | ||
(cached_libelle, cached_nom_complet, id, siret, created_at, updated_at) | ||
VALUES | ||
('Necron', 'Necrontyr', 1, '⚰️', '1967-12-19', '1967-12-19') | ||
; | ||
`; | ||
await pg.sql` | ||
INSERT INTO users | ||
(id, email, created_at, updated_at, given_name, family_name, phone_number, job) | ||
VALUES | ||
(1, '[email protected]', '4444-04-04', '4444-04-04', 'lion', 'el''jonson', 'i', 'primarque') | ||
; | ||
`; | ||
FakeTimers.install({ now: new Date("4444-04-04") }); | ||
|
||
const userOrganizationLink = await linkUserToOrganization({ | ||
organization_id: 1, | ||
user_id: 1, | ||
verification_type: "bypassed", | ||
}); | ||
|
||
expect(userOrganizationLink).to.deep.equal({ | ||
created_at: new Date("4444-04-04"), | ||
has_been_greeted: false, | ||
is_external: false, | ||
needs_official_contact_email_verification: false, | ||
official_contact_email_verification_sent_at: null, | ||
official_contact_email_verification_token: null, | ||
organization_id: 1, | ||
updated_at: new Date("4444-04-04"), | ||
user_id: 1, | ||
verification_type: "bypassed", | ||
verified_at: null, | ||
}); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
packages/identite/src/repositories/organization/link-user-to-organization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { DatabaseContext, UserOrganizationLink } from "#src/types"; | ||
import type { QueryResult } from "pg"; | ||
|
||
export function linkUserToOrganizationFactory({ pg }: DatabaseContext) { | ||
return async function linkUserToOrganization({ | ||
organization_id, | ||
user_id, | ||
is_external = false, | ||
verification_type, | ||
needs_official_contact_email_verification = false, | ||
}: { | ||
organization_id: number; | ||
user_id: number; | ||
is_external?: boolean; | ||
verification_type: UserOrganizationLink["verification_type"]; | ||
needs_official_contact_email_verification?: UserOrganizationLink["needs_official_contact_email_verification"]; | ||
}) { | ||
const { rows }: QueryResult<UserOrganizationLink> = await pg.query( | ||
` | ||
INSERT INTO users_organizations | ||
(user_id, | ||
organization_id, | ||
is_external, | ||
verification_type, | ||
needs_official_contact_email_verification, | ||
updated_at, | ||
created_at) | ||
VALUES | ||
($1, $2, $3, $4, $5, $6, $7) | ||
RETURNING * | ||
`, | ||
[ | ||
user_id, | ||
organization_id, | ||
is_external, | ||
verification_type, | ||
needs_official_contact_email_verification, | ||
new Date(), | ||
new Date(), | ||
], | ||
); | ||
|
||
return rows.shift()! as UserOrganizationLink; | ||
}; | ||
} | ||
|
||
export type LinkUserToOrganizationHandler = ReturnType< | ||
typeof linkUserToOrganizationFactory | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.