Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tsc): allow entry point of type library #695

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
node-version-file: package.json
- run: CYPRESS_INSTALL_BINARY=0 npm ci --include=dev
- run: npm run test:lint
- run: npm run test:type-check
- run: npm exec tsc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Est ce qu'une compilation implique un type check ? qu'est ce qui est testé en plus ?

- run: npm run test:unit
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
"imports": {
"#cypress/*": "./cypress/*.ts"
},
"exports": {
"./connectors/*": {
"default": "./build/connectors/*.js"
},
"./managers/*": {
"default": "./build/managers/*.js"
},
"./types": {
"default": "./src/types/index.d.ts"
},
".": {
"default": "./src/index.ts"
}
},
"main": "src/index.js",
"scripts": {
"build": "run-s build:**",
Expand Down Expand Up @@ -52,6 +66,7 @@
"@types/ejs": "^3.1.5",
"@types/express": "^4.17.21",
"@types/express-session": "^1.18.0",
"@types/lodash-es": "^4.17.12",
"@types/morgan": "^1.9.9",
"@zootools/email-spell-checker": "^1.12.0",
"await-to-js": "^3.0.0",
Expand Down Expand Up @@ -99,7 +114,6 @@
"@types/chai-as-promised": "^7.1.8",
"@types/http-errors": "^2.0.4",
"@types/lodash": "^4.17.0",
"@types/lodash-es": "^4.17.12",
"@types/mocha": "^10.0.7",
"@types/node": "^22.1.0",
"@types/oidc-provider": "^8.5.2",
Expand Down
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-es";
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;
},
),
);
};
14 changes: 11 additions & 3 deletions src/managers/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from "@simplewebauthn/server";
import type {
AuthenticationResponseJSON,
PublicKeyCredentialCreationOptionsJSON,
PublicKeyCredentialRequestOptionsJSON,
RegistrationResponseJSON,
} from "@simplewebauthn/types";
import { isEmpty } from "lodash-es";
Expand Down Expand Up @@ -115,7 +117,10 @@ export const deleteUserAuthenticator = async (
return true;
};

export const getRegistrationOptions = async (email: string) => {
export const getRegistrationOptions: (email: string) => Promise<{
updatedUser: User;
registrationOptions: PublicKeyCredentialCreationOptionsJSON;
}> = async (email) => {
const user = await findUserByEmail(email);

if (isEmpty(user)) {
Expand Down Expand Up @@ -232,10 +237,13 @@ export const verifyRegistration = async ({
return { userVerified: user_verified, user: await enableForce2fa(user.id) };
};

export const getAuthenticationOptions = async (
export const getAuthenticationOptions: (
email: string | undefined,
isSecondFactorAuthentication: boolean,
) => {
) => Promise<{
updatedUser: User;
authenticationOptions: PublicKeyCredentialRequestOptionsJSON;
}> = async (email, isSecondFactorAuthentication) => {
if (!email) {
throw new NotFoundError();
}
Expand Down
13 changes: 12 additions & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/// <reference path="./authenticator.d.ts" />
/// <reference path="./connection.d.ts" />
/// <reference path="./email-domain.d.ts" />
/// <reference path="./express-session.d.ts" />
/// <reference path="./user.d.ts" />
/// <reference path="./is-disposable-email-domain.d.ts" />
/// <reference path="./moderation.d.ts" />
/// <reference path="./oidc-client.d.ts" />
/// <reference path="./oidc-provider.d.ts" />
/// <reference path="./organization-info.d.ts" />
/// <reference path="./organization.d.ts" />
/// <reference path="./request.d.ts" />
/// <reference path="./tld-extract.d.ts" />
/// <reference path="./user-organization-link.d.ts" />
/// <reference path="./user.d.ts" />
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"module": "Preserve",
Expand Down