Skip to content

Commit

Permalink
debug: move markDomainAsVerified
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil committed Sep 17, 2024
1 parent 36ff2e4 commit 64c63f3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import notificationMessages from "../config/notification-messages";
import { getOrganizationInfo } from "../connectors/api-sirene";
import { sendModerationProcessedEmail } from "../managers/moderation";
import { forceJoinOrganization } from "../managers/organization/join";
import { markDomainAsVerified } from "../managers/organization/main";
import { markDomainAsVerified } from "../managers/organization/markDomainAsVerified";
import { getUserOrganizationLink } from "../repositories/organization/getters";
import {
idSchema,
Expand Down
3 changes: 2 additions & 1 deletion src/managers/organization/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ import {
} from "../../services/organization";
import { isEmailValid } from "../../services/security";
import { unableToAutoJoinOrganizationMd } from "../../views/mails/unable-to-auto-join-organization";
import { getOrganizationsByUserId, markDomainAsVerified } from "./main";
import { getOrganizationsByUserId } from "./main";
import { markDomainAsVerified } from "./markDomainAsVerified";

export const doSuggestOrganizations = async ({
user_id,
Expand Down
63 changes: 2 additions & 61 deletions src/managers/organization/main.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { isEmpty, some } from "lodash-es";
import { isEmpty } from "lodash-es";
import { NotFoundError } from "../../config/errors";
import {
addDomain,
findEmailDomainsByOrganizationId,
} from "../../repositories/email-domain";
import {
findByUserId,
findById as findOrganizationById,
findPendingByUserId,
getUsers,
} from "../../repositories/organization/getters";
import {
deleteUserOrganization,
updateUserOrganizationLink,
} from "../../repositories/organization/setters";
import { deleteUserOrganization } from "../../repositories/organization/setters";
import { setSelectedOrganizationId } from "../../repositories/redis/selected-organization";
import { getEmailDomain } from "../../services/email";

export const getOrganizationsByUserId = findByUserId;
export const getOrganizationById = findOrganizationById;
Expand Down Expand Up @@ -50,56 +41,6 @@ export const quitOrganization = async ({

return true;
};
export const markDomainAsVerified = async ({
organization_id,
domain,
domain_verification_type,
}: {
organization_id: number;
domain: string;
domain_verification_type: EmailDomain["verification_type"];
}) => {
const organization = await findOrganizationById(organization_id);
if (isEmpty(organization)) {
throw new NotFoundError();
}
const emailDomains = await findEmailDomainsByOrganizationId(organization_id);

if (
!some(emailDomains, { domain, verification_type: domain_verification_type })
) {
await addDomain({
organization_id,
domain,
verification_type: domain_verification_type,
});
}

const usersInOrganization = await getUsers(organization_id);

await Promise.all(
usersInOrganization.map(
({ id, email, verification_type: link_verification_type }) => {
const userDomain = getEmailDomain(email);
if (
userDomain === domain &&
[
null,
"no_verification_means_available",
"no_verification_means_for_entreprise_unipersonnelle",
].includes(link_verification_type)
) {
return updateUserOrganizationLink(organization_id, id, {
verification_type: "domain",
});
}

return null;
},
),
);
};

export const selectOrganization = async ({
user_id,
organization_id,
Expand Down
61 changes: 61 additions & 0 deletions src/managers/organization/markDomainAsVerified.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { isEmpty, some } from "lodash";
import { NotFoundError } from "../../config/errors";
import {
addDomain,
findEmailDomainsByOrganizationId,
} from "../../repositories/email-domain";
import { findById, getUsers } from "../../repositories/organization/getters";
import { updateUserOrganizationLink } from "../../repositories/organization/setters";
import { getEmailDomain } from "../../services/email";

//

export const markDomainAsVerified = async ({
organization_id,
domain,
domain_verification_type,
}: {
organization_id: number;
domain: string;
domain_verification_type: EmailDomain["verification_type"];
}) => {
const organization = await findById(organization_id);
if (isEmpty(organization)) {
throw new NotFoundError();
}
const emailDomains = await findEmailDomainsByOrganizationId(organization_id);

if (
!some(emailDomains, { domain, verification_type: domain_verification_type })
) {
await addDomain({
organization_id,
domain,
verification_type: domain_verification_type,
});
}

const usersInOrganization = await getUsers(organization_id);

await Promise.all(
usersInOrganization.map(
({ id, email, verification_type: link_verification_type }) => {
const userDomain = getEmailDomain(email);
if (
userDomain === domain &&
[
null,
"no_verification_means_available",
"no_verification_means_for_entreprise_unipersonnelle",
].includes(link_verification_type)
) {
return updateUserOrganizationLink(organization_id, id, {
verification_type: "domain",
});
}

return null;
},
),
);
};

0 comments on commit 64c63f3

Please sign in to comment.