-
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 ecfccb2
Showing
22 changed files
with
160 additions
and
58 deletions.
There are no files selected for viewing
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 {} |
4 changes: 4 additions & 0 deletions
4
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,4 @@ | ||
import { describe } from "mocha"; | ||
import { forceJoinOrganizationFactory } from "./force-join-organization.js"; | ||
|
||
describe(forceJoinOrganizationFactory.name, () => {}); |
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
4 changes: 4 additions & 0 deletions
4
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,4 @@ | ||
import { describe } from "mocha"; | ||
import { linkUserToOrganizationFactory } from "./link-user-to-organization.js"; | ||
|
||
describe(linkUserToOrganizationFactory.name, () => {}); |
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
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
2 changes: 1 addition & 1 deletion
2
src/managers/organization/official-contact-email-verification.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
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