@intlayer/backend 5.5.11 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/cjs/controllers/ai.controller.cjs +10 -7
- package/dist/cjs/controllers/ai.controller.cjs.map +1 -1
- package/dist/cjs/controllers/dictionary.controller.cjs +50 -58
- package/dist/cjs/controllers/dictionary.controller.cjs.map +1 -1
- package/dist/cjs/controllers/eventListener.controller.cjs +2 -18
- package/dist/cjs/controllers/eventListener.controller.cjs.map +1 -1
- package/dist/cjs/controllers/newsletter.controller.cjs +38 -3
- package/dist/cjs/controllers/newsletter.controller.cjs.map +1 -1
- package/dist/cjs/controllers/oAuth2.controller.cjs +3 -3
- package/dist/cjs/controllers/oAuth2.controller.cjs.map +1 -1
- package/dist/cjs/controllers/organization.controller.cjs +92 -106
- package/dist/cjs/controllers/organization.controller.cjs.map +1 -1
- package/dist/cjs/controllers/project.controller.cjs +81 -83
- package/dist/cjs/controllers/project.controller.cjs.map +1 -1
- package/dist/cjs/controllers/projectAccessKey.controller.cjs +30 -24
- package/dist/cjs/controllers/projectAccessKey.controller.cjs.map +1 -1
- package/dist/cjs/controllers/search.controller.cjs.map +1 -1
- package/dist/cjs/controllers/stripe.controller.cjs +4 -25
- package/dist/cjs/controllers/stripe.controller.cjs.map +1 -1
- package/dist/cjs/controllers/tag.controller.cjs +27 -16
- package/dist/cjs/controllers/tag.controller.cjs.map +1 -1
- package/dist/cjs/controllers/user.controller.cjs +88 -24
- package/dist/cjs/controllers/user.controller.cjs.map +1 -1
- package/dist/cjs/emails/InviteUserEmail.cjs +30 -12
- package/dist/cjs/emails/InviteUserEmail.cjs.map +1 -1
- package/dist/cjs/emails/OAuthTokenCreatedEmail.cjs +266 -0
- package/dist/cjs/emails/OAuthTokenCreatedEmail.cjs.map +1 -0
- package/dist/cjs/emails/ResetUserPassword.cjs +27 -15
- package/dist/cjs/emails/ResetUserPassword.cjs.map +1 -1
- package/dist/cjs/emails/ValidateUserEmail.cjs +27 -36
- package/dist/cjs/emails/ValidateUserEmail.cjs.map +1 -1
- package/dist/cjs/emails/Welcome.cjs +27 -15
- package/dist/cjs/emails/Welcome.cjs.map +1 -1
- package/dist/cjs/emails/index.cjs +7 -5
- package/dist/cjs/emails/index.cjs.map +1 -1
- package/dist/cjs/export.cjs +2 -5
- package/dist/cjs/export.cjs.map +1 -1
- package/dist/cjs/index.cjs +61 -111
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/middlewares/oAuth2.middleware.cjs +26 -38
- package/dist/cjs/middlewares/oAuth2.middleware.cjs.map +1 -1
- package/dist/cjs/middlewares/request.middleware.cjs.map +1 -1
- package/dist/cjs/middlewares/sessionAuth.middleware.cjs +17 -138
- package/dist/cjs/middlewares/sessionAuth.middleware.cjs.map +1 -1
- package/dist/cjs/models/dictionary.model.cjs.map +1 -1
- package/dist/cjs/models/discussion.model.cjs.map +1 -1
- package/dist/cjs/models/oAuth2.model.cjs +4 -1
- package/dist/cjs/models/oAuth2.model.cjs.map +1 -1
- package/dist/cjs/models/organization.model.cjs +1 -4
- package/dist/cjs/models/organization.model.cjs.map +1 -1
- package/dist/cjs/models/project.model.cjs +4 -1
- package/dist/cjs/models/project.model.cjs.map +1 -1
- package/dist/cjs/models/session.model.cjs +34 -0
- package/dist/cjs/models/session.model.cjs.map +1 -0
- package/dist/cjs/models/tag.model.cjs.map +1 -1
- package/dist/cjs/models/user.model.cjs.map +1 -1
- package/dist/cjs/routes/ai.routes.cjs +3 -1
- package/dist/cjs/routes/ai.routes.cjs.map +1 -1
- package/dist/cjs/routes/dictionary.routes.cjs.map +1 -1
- package/dist/cjs/routes/eventListener.routes.cjs +1 -1
- package/dist/cjs/routes/eventListener.routes.cjs.map +1 -1
- package/dist/cjs/routes/newsletter.routes.cjs.map +1 -1
- package/dist/cjs/routes/organization.routes.cjs +8 -8
- package/dist/cjs/routes/organization.routes.cjs.map +1 -1
- package/dist/cjs/routes/project.routes.cjs +23 -14
- package/dist/cjs/routes/project.routes.cjs.map +1 -1
- package/dist/cjs/routes/search.routes.cjs.map +1 -1
- package/dist/cjs/routes/stripe.routes.cjs.map +1 -1
- package/dist/cjs/routes/tags.routes.cjs +4 -4
- package/dist/cjs/routes/tags.routes.cjs.map +1 -1
- package/dist/cjs/routes/user.routes.cjs +12 -12
- package/dist/cjs/routes/user.routes.cjs.map +1 -1
- package/dist/cjs/schemas/dictionary.schema.cjs +18 -1
- package/dist/cjs/schemas/dictionary.schema.cjs.map +1 -1
- package/dist/cjs/schemas/discussion.schema.cjs +18 -1
- package/dist/cjs/schemas/discussion.schema.cjs.map +1 -1
- package/dist/cjs/schemas/oAuth2.schema.cjs +18 -1
- package/dist/cjs/schemas/oAuth2.schema.cjs.map +1 -1
- package/dist/cjs/schemas/organization.schema.cjs +21 -1
- package/dist/cjs/schemas/organization.schema.cjs.map +1 -1
- package/dist/cjs/schemas/plans.schema.cjs +18 -1
- package/dist/cjs/schemas/plans.schema.cjs.map +1 -1
- package/dist/cjs/schemas/project.schema.cjs +19 -14
- package/dist/cjs/schemas/project.schema.cjs.map +1 -1
- package/dist/cjs/schemas/session.schema.cjs +63 -0
- package/dist/cjs/schemas/session.schema.cjs.map +1 -0
- package/dist/cjs/schemas/tag.schema.cjs +18 -1
- package/dist/cjs/schemas/tag.schema.cjs.map +1 -1
- package/dist/cjs/schemas/user.schema.cjs +18 -48
- package/dist/cjs/schemas/user.schema.cjs.map +1 -1
- package/dist/cjs/services/dictionary.service.cjs +6 -5
- package/dist/cjs/services/dictionary.service.cjs.map +1 -1
- package/dist/cjs/services/email.service.cjs +13 -0
- package/dist/cjs/services/email.service.cjs.map +1 -1
- package/dist/cjs/services/oAuth2.service.cjs +49 -10
- package/dist/cjs/services/oAuth2.service.cjs.map +1 -1
- package/dist/cjs/services/organization.service.cjs +16 -15
- package/dist/cjs/services/organization.service.cjs.map +1 -1
- package/dist/cjs/services/project.service.cjs +1 -1
- package/dist/cjs/services/project.service.cjs.map +1 -1
- package/dist/cjs/services/projectAccessKey.service.cjs +17 -33
- package/dist/cjs/services/projectAccessKey.service.cjs.map +1 -1
- package/dist/cjs/services/subscription.service.cjs +10 -10
- package/dist/cjs/services/subscription.service.cjs.map +1 -1
- package/dist/cjs/services/tag.service.cjs.map +1 -1
- package/dist/cjs/services/user.service.cjs +2 -42
- package/dist/cjs/services/user.service.cjs.map +1 -1
- package/dist/cjs/types/dictionary.types.cjs.map +1 -1
- package/dist/cjs/types/discussion.types.cjs.map +1 -1
- package/dist/cjs/types/oAuth2.types.cjs.map +1 -1
- package/dist/cjs/types/organization.types.cjs.map +1 -1
- package/dist/cjs/types/plan.types.cjs.map +1 -1
- package/dist/cjs/types/project.types.cjs.map +1 -1
- package/dist/cjs/types/session.types.cjs.map +1 -1
- package/dist/cjs/types/tag.types.cjs.map +1 -1
- package/dist/cjs/types/user.types.cjs.map +1 -1
- package/dist/cjs/utils/AI/aiSdk.cjs.map +1 -1
- package/dist/cjs/utils/AI/askDocQuestion/PROMPT.md +7 -1
- package/dist/cjs/utils/AI/askDocQuestion/askDocQuestion.cjs +32 -14
- package/dist/cjs/utils/AI/askDocQuestion/askDocQuestion.cjs.map +1 -1
- package/dist/cjs/utils/AI/askDocQuestion/embeddings.json +88086 -76800
- package/dist/cjs/utils/AI/autocomplete/PROMPT.md +18 -2
- package/dist/cjs/utils/AI/autocomplete/index.cjs +8 -5
- package/dist/cjs/utils/AI/autocomplete/index.cjs.map +1 -1
- package/dist/cjs/utils/access.cjs +2 -0
- package/dist/cjs/utils/access.cjs.map +1 -0
- package/dist/cjs/utils/accessControl.cjs +7 -0
- package/dist/cjs/utils/accessControl.cjs.map +1 -1
- package/dist/cjs/utils/auth/getAuth.cjs +248 -0
- package/dist/cjs/utils/auth/getAuth.cjs.map +1 -0
- package/dist/cjs/utils/cors.cjs +55 -0
- package/dist/cjs/utils/cors.cjs.map +1 -0
- package/dist/cjs/utils/ensureMongoDocumentToObject.cjs.map +1 -1
- package/dist/cjs/utils/errors/ErrorHandler.cjs +2 -2
- package/dist/cjs/utils/errors/ErrorHandler.cjs.map +1 -1
- package/dist/cjs/utils/errors/errorCodes.cjs +114 -153
- package/dist/cjs/utils/errors/errorCodes.cjs.map +1 -1
- package/dist/cjs/utils/filtersAndPagination/getOrganizationFiltersAndPagination.cjs.map +1 -1
- package/dist/cjs/utils/filtersAndPagination/getProjectFiltersAndPagination.cjs.map +1 -1
- package/dist/cjs/utils/filtersAndPagination/getTagFiltersAndPagination.cjs.map +1 -1
- package/dist/cjs/utils/filtersAndPagination/getUserFiltersAndPagination.cjs +1 -1
- package/dist/cjs/utils/filtersAndPagination/getUserFiltersAndPagination.cjs.map +1 -1
- package/dist/cjs/utils/mapper/dictionary.cjs.map +1 -1
- package/dist/cjs/utils/mapper/organization.cjs +10 -8
- package/dist/cjs/utils/mapper/organization.cjs.map +1 -1
- package/dist/cjs/utils/mapper/project.cjs +5 -18
- package/dist/cjs/utils/mapper/project.cjs.map +1 -1
- package/dist/cjs/utils/mapper/tag.cjs +4 -2
- package/dist/cjs/utils/mapper/tag.cjs.map +1 -1
- package/dist/cjs/utils/mapper/user.cjs +6 -3
- package/dist/cjs/utils/mapper/user.cjs.map +1 -1
- package/dist/cjs/utils/mergeFunctionTypes.cjs +17 -0
- package/dist/cjs/utils/mergeFunctionTypes.cjs.map +1 -0
- package/dist/cjs/utils/mongoDB/connectDB.cjs +3 -1
- package/dist/cjs/utils/mongoDB/connectDB.cjs.map +1 -1
- package/dist/cjs/utils/mongoDB/types.cjs +17 -0
- package/dist/cjs/utils/mongoDB/types.cjs.map +1 -0
- package/dist/cjs/utils/oAuth2.cjs.map +1 -1
- package/dist/cjs/utils/permissions.cjs +166 -0
- package/dist/cjs/utils/permissions.cjs.map +1 -0
- package/dist/cjs/utils/rateLimiter.cjs +88 -0
- package/dist/cjs/utils/rateLimiter.cjs.map +1 -0
- package/dist/esm/controllers/ai.controller.mjs +10 -7
- package/dist/esm/controllers/ai.controller.mjs.map +1 -1
- package/dist/esm/controllers/dictionary.controller.mjs +50 -58
- package/dist/esm/controllers/dictionary.controller.mjs.map +1 -1
- package/dist/esm/controllers/eventListener.controller.mjs +2 -8
- package/dist/esm/controllers/eventListener.controller.mjs.map +1 -1
- package/dist/esm/controllers/newsletter.controller.mjs +38 -3
- package/dist/esm/controllers/newsletter.controller.mjs.map +1 -1
- package/dist/esm/controllers/oAuth2.controller.mjs +2 -2
- package/dist/esm/controllers/oAuth2.controller.mjs.map +1 -1
- package/dist/esm/controllers/organization.controller.mjs +95 -106
- package/dist/esm/controllers/organization.controller.mjs.map +1 -1
- package/dist/esm/controllers/project.controller.mjs +81 -83
- package/dist/esm/controllers/project.controller.mjs.map +1 -1
- package/dist/esm/controllers/projectAccessKey.controller.mjs +30 -24
- package/dist/esm/controllers/projectAccessKey.controller.mjs.map +1 -1
- package/dist/esm/controllers/search.controller.mjs.map +1 -1
- package/dist/esm/controllers/stripe.controller.mjs +4 -25
- package/dist/esm/controllers/stripe.controller.mjs.map +1 -1
- package/dist/esm/controllers/tag.controller.mjs +27 -16
- package/dist/esm/controllers/tag.controller.mjs.map +1 -1
- package/dist/esm/controllers/user.controller.mjs +85 -22
- package/dist/esm/controllers/user.controller.mjs.map +1 -1
- package/dist/esm/emails/InviteUserEmail.mjs +32 -14
- package/dist/esm/emails/InviteUserEmail.mjs.map +1 -1
- package/dist/esm/emails/OAuthTokenCreatedEmail.mjs +254 -0
- package/dist/esm/emails/OAuthTokenCreatedEmail.mjs.map +1 -0
- package/dist/esm/emails/ResetUserPassword.mjs +29 -17
- package/dist/esm/emails/ResetUserPassword.mjs.map +1 -1
- package/dist/esm/emails/ValidateUserEmail.mjs +29 -38
- package/dist/esm/emails/ValidateUserEmail.mjs.map +1 -1
- package/dist/esm/emails/Welcome.mjs +29 -17
- package/dist/esm/emails/Welcome.mjs.map +1 -1
- package/dist/esm/emails/index.mjs +3 -2
- package/dist/esm/emails/index.mjs.map +1 -1
- package/dist/esm/export.mjs +1 -3
- package/dist/esm/export.mjs.map +1 -1
- package/dist/esm/index.mjs +60 -111
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/middlewares/oAuth2.middleware.mjs +27 -36
- package/dist/esm/middlewares/oAuth2.middleware.mjs.map +1 -1
- package/dist/esm/middlewares/request.middleware.mjs.map +1 -1
- package/dist/esm/middlewares/sessionAuth.middleware.mjs +16 -127
- package/dist/esm/middlewares/sessionAuth.middleware.mjs.map +1 -1
- package/dist/esm/models/dictionary.model.mjs.map +1 -1
- package/dist/esm/models/discussion.model.mjs.map +1 -1
- package/dist/esm/models/oAuth2.model.mjs +4 -1
- package/dist/esm/models/oAuth2.model.mjs.map +1 -1
- package/dist/esm/models/organization.model.mjs +1 -4
- package/dist/esm/models/organization.model.mjs.map +1 -1
- package/dist/esm/models/project.model.mjs +4 -1
- package/dist/esm/models/project.model.mjs.map +1 -1
- package/dist/esm/models/session.model.mjs +10 -0
- package/dist/esm/models/session.model.mjs.map +1 -0
- package/dist/esm/models/tag.model.mjs.map +1 -1
- package/dist/esm/models/user.model.mjs.map +1 -1
- package/dist/esm/routes/ai.routes.mjs +3 -1
- package/dist/esm/routes/ai.routes.mjs.map +1 -1
- package/dist/esm/routes/dictionary.routes.mjs.map +1 -1
- package/dist/esm/routes/eventListener.routes.mjs +1 -1
- package/dist/esm/routes/eventListener.routes.mjs.map +1 -1
- package/dist/esm/routes/newsletter.routes.mjs.map +1 -1
- package/dist/esm/routes/organization.routes.mjs +8 -8
- package/dist/esm/routes/organization.routes.mjs.map +1 -1
- package/dist/esm/routes/project.routes.mjs +23 -14
- package/dist/esm/routes/project.routes.mjs.map +1 -1
- package/dist/esm/routes/search.routes.mjs.map +1 -1
- package/dist/esm/routes/stripe.routes.mjs.map +1 -1
- package/dist/esm/routes/tags.routes.mjs +4 -4
- package/dist/esm/routes/tags.routes.mjs.map +1 -1
- package/dist/esm/routes/user.routes.mjs +14 -14
- package/dist/esm/routes/user.routes.mjs.map +1 -1
- package/dist/esm/schemas/dictionary.schema.mjs +18 -1
- package/dist/esm/schemas/dictionary.schema.mjs.map +1 -1
- package/dist/esm/schemas/discussion.schema.mjs +18 -1
- package/dist/esm/schemas/discussion.schema.mjs.map +1 -1
- package/dist/esm/schemas/oAuth2.schema.mjs +18 -1
- package/dist/esm/schemas/oAuth2.schema.mjs.map +1 -1
- package/dist/esm/schemas/organization.schema.mjs +21 -1
- package/dist/esm/schemas/organization.schema.mjs.map +1 -1
- package/dist/esm/schemas/plans.schema.mjs +18 -1
- package/dist/esm/schemas/plans.schema.mjs.map +1 -1
- package/dist/esm/schemas/project.schema.mjs +21 -15
- package/dist/esm/schemas/project.schema.mjs.map +1 -1
- package/dist/esm/schemas/session.schema.mjs +39 -0
- package/dist/esm/schemas/session.schema.mjs.map +1 -0
- package/dist/esm/schemas/tag.schema.mjs +21 -4
- package/dist/esm/schemas/tag.schema.mjs.map +1 -1
- package/dist/esm/schemas/user.schema.mjs +18 -48
- package/dist/esm/schemas/user.schema.mjs.map +1 -1
- package/dist/esm/services/dictionary.service.mjs +6 -5
- package/dist/esm/services/dictionary.service.mjs.map +1 -1
- package/dist/esm/services/email.service.mjs +33 -16
- package/dist/esm/services/email.service.mjs.map +1 -1
- package/dist/esm/services/oAuth2.service.mjs +47 -10
- package/dist/esm/services/oAuth2.service.mjs.map +1 -1
- package/dist/esm/services/organization.service.mjs +16 -14
- package/dist/esm/services/organization.service.mjs.map +1 -1
- package/dist/esm/services/project.service.mjs +1 -1
- package/dist/esm/services/project.service.mjs.map +1 -1
- package/dist/esm/services/projectAccessKey.service.mjs +15 -31
- package/dist/esm/services/projectAccessKey.service.mjs.map +1 -1
- package/dist/esm/services/subscription.service.mjs +10 -10
- package/dist/esm/services/subscription.service.mjs.map +1 -1
- package/dist/esm/services/tag.service.mjs.map +1 -1
- package/dist/esm/services/user.service.mjs +2 -40
- package/dist/esm/services/user.service.mjs.map +1 -1
- package/dist/esm/types/user.types.mjs.map +1 -1
- package/dist/esm/utils/AI/aiSdk.mjs.map +1 -1
- package/dist/esm/utils/AI/askDocQuestion/PROMPT.md +7 -1
- package/dist/esm/utils/AI/askDocQuestion/askDocQuestion.mjs +32 -14
- package/dist/esm/utils/AI/askDocQuestion/askDocQuestion.mjs.map +1 -1
- package/dist/esm/utils/AI/askDocQuestion/embeddings.json +88086 -76800
- package/dist/esm/utils/AI/autocomplete/PROMPT.md +18 -2
- package/dist/esm/utils/AI/autocomplete/index.mjs +8 -5
- package/dist/esm/utils/AI/autocomplete/index.mjs.map +1 -1
- package/dist/esm/utils/access.mjs +1 -0
- package/dist/esm/utils/access.mjs.map +1 -0
- package/dist/esm/utils/accessControl.mjs +7 -0
- package/dist/esm/utils/accessControl.mjs.map +1 -1
- package/dist/esm/utils/auth/getAuth.mjs +227 -0
- package/dist/esm/utils/auth/getAuth.mjs.map +1 -0
- package/dist/esm/utils/cors.mjs +31 -0
- package/dist/esm/utils/cors.mjs.map +1 -0
- package/dist/esm/utils/ensureMongoDocumentToObject.mjs.map +1 -1
- package/dist/esm/utils/errors/ErrorHandler.mjs +2 -2
- package/dist/esm/utils/errors/ErrorHandler.mjs.map +1 -1
- package/dist/esm/utils/errors/errorCodes.mjs +114 -153
- package/dist/esm/utils/errors/errorCodes.mjs.map +1 -1
- package/dist/esm/utils/filtersAndPagination/getOrganizationFiltersAndPagination.mjs.map +1 -1
- package/dist/esm/utils/filtersAndPagination/getProjectFiltersAndPagination.mjs.map +1 -1
- package/dist/esm/utils/filtersAndPagination/getTagFiltersAndPagination.mjs.map +1 -1
- package/dist/esm/utils/filtersAndPagination/getUserFiltersAndPagination.mjs +1 -1
- package/dist/esm/utils/filtersAndPagination/getUserFiltersAndPagination.mjs.map +1 -1
- package/dist/esm/utils/mapper/dictionary.mjs.map +1 -1
- package/dist/esm/utils/mapper/organization.mjs +8 -7
- package/dist/esm/utils/mapper/organization.mjs.map +1 -1
- package/dist/esm/utils/mapper/project.mjs +5 -18
- package/dist/esm/utils/mapper/project.mjs.map +1 -1
- package/dist/esm/utils/mapper/tag.mjs +4 -2
- package/dist/esm/utils/mapper/tag.mjs.map +1 -1
- package/dist/esm/utils/mapper/user.mjs +6 -3
- package/dist/esm/utils/mapper/user.mjs.map +1 -1
- package/dist/esm/utils/mergeFunctionTypes.mjs +1 -0
- package/dist/esm/utils/mergeFunctionTypes.mjs.map +1 -0
- package/dist/esm/utils/mongoDB/connectDB.mjs +3 -1
- package/dist/esm/utils/mongoDB/connectDB.mjs.map +1 -1
- package/dist/esm/utils/mongoDB/types.mjs +1 -0
- package/dist/esm/utils/mongoDB/types.mjs.map +1 -0
- package/dist/esm/utils/oAuth2.mjs +3 -3
- package/dist/esm/utils/oAuth2.mjs.map +1 -1
- package/dist/esm/utils/permissions.mjs +138 -0
- package/dist/esm/utils/permissions.mjs.map +1 -0
- package/dist/esm/utils/rateLimiter.mjs +53 -0
- package/dist/esm/utils/rateLimiter.mjs.map +1 -0
- package/dist/types/controllers/ai.controller.d.ts +12 -10
- package/dist/types/controllers/ai.controller.d.ts.map +1 -1
- package/dist/types/controllers/dictionary.controller.d.ts +8 -9
- package/dist/types/controllers/dictionary.controller.d.ts.map +1 -1
- package/dist/types/controllers/eventListener.controller.d.ts +2 -3
- package/dist/types/controllers/eventListener.controller.d.ts.map +1 -1
- package/dist/types/controllers/newsletter.controller.d.ts +5 -6
- package/dist/types/controllers/newsletter.controller.d.ts.map +1 -1
- package/dist/types/controllers/oAuth2.controller.d.ts +3 -3
- package/dist/types/controllers/oAuth2.controller.d.ts.map +1 -1
- package/dist/types/controllers/organization.controller.d.ts +22 -23
- package/dist/types/controllers/organization.controller.d.ts.map +1 -1
- package/dist/types/controllers/project.controller.d.ts +13 -14
- package/dist/types/controllers/project.controller.d.ts.map +1 -1
- package/dist/types/controllers/projectAccessKey.controller.d.ts +5 -6
- package/dist/types/controllers/projectAccessKey.controller.d.ts.map +1 -1
- package/dist/types/controllers/search.controller.d.ts +2 -3
- package/dist/types/controllers/search.controller.d.ts.map +1 -1
- package/dist/types/controllers/stripe.controller.d.ts +5 -6
- package/dist/types/controllers/stripe.controller.d.ts.map +1 -1
- package/dist/types/controllers/tag.controller.d.ts +9 -10
- package/dist/types/controllers/tag.controller.d.ts.map +1 -1
- package/dist/types/controllers/user.controller.d.ts +16 -19
- package/dist/types/controllers/user.controller.d.ts.map +1 -1
- package/dist/types/emails/InviteUserEmail.d.ts.map +1 -1
- package/dist/types/emails/OAuthTokenCreatedEmail.d.ts +21 -0
- package/dist/types/emails/OAuthTokenCreatedEmail.d.ts.map +1 -0
- package/dist/types/emails/ResetUserPassword.d.ts.map +1 -1
- package/dist/types/emails/Welcome.d.ts.map +1 -1
- package/dist/types/emails/index.d.ts +3 -2
- package/dist/types/emails/index.d.ts.map +1 -1
- package/dist/types/export.d.ts +2 -3
- package/dist/types/export.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/middlewares/oAuth2.middleware.d.ts +1 -2
- package/dist/types/middlewares/oAuth2.middleware.d.ts.map +1 -1
- package/dist/types/middlewares/request.middleware.d.ts +3 -3
- package/dist/types/middlewares/request.middleware.d.ts.map +1 -1
- package/dist/types/middlewares/sessionAuth.middleware.d.ts +3 -25
- package/dist/types/middlewares/sessionAuth.middleware.d.ts.map +1 -1
- package/dist/types/models/dictionary.model.d.ts +6 -5
- package/dist/types/models/dictionary.model.d.ts.map +1 -1
- package/dist/types/models/discussion.model.d.ts +7 -2
- package/dist/types/models/discussion.model.d.ts.map +1 -1
- package/dist/types/models/oAuth2.model.d.ts +3 -2
- package/dist/types/models/oAuth2.model.d.ts.map +1 -1
- package/dist/types/models/organization.model.d.ts +2 -12
- package/dist/types/models/organization.model.d.ts.map +1 -1
- package/dist/types/models/project.model.d.ts +2 -11
- package/dist/types/models/project.model.d.ts.map +1 -1
- package/dist/types/models/session.model.d.ts +3 -0
- package/dist/types/models/session.model.d.ts.map +1 -0
- package/dist/types/models/tag.model.d.ts.map +1 -1
- package/dist/types/models/user.model.d.ts.map +1 -1
- package/dist/types/routes/ai.routes.d.ts.map +1 -1
- package/dist/types/routes/organization.routes.d.ts +4 -4
- package/dist/types/routes/project.routes.d.ts +4 -4
- package/dist/types/routes/project.routes.d.ts.map +1 -1
- package/dist/types/routes/tags.routes.d.ts +2 -2
- package/dist/types/routes/user.routes.d.ts +6 -7
- package/dist/types/routes/user.routes.d.ts.map +1 -1
- package/dist/types/schemas/dictionary.schema.d.ts +6 -8
- package/dist/types/schemas/dictionary.schema.d.ts.map +1 -1
- package/dist/types/schemas/discussion.schema.d.ts +7 -5
- package/dist/types/schemas/discussion.schema.d.ts.map +1 -1
- package/dist/types/schemas/oAuth2.schema.d.ts +4 -3
- package/dist/types/schemas/oAuth2.schema.d.ts.map +1 -1
- package/dist/types/schemas/organization.schema.d.ts +6 -10
- package/dist/types/schemas/organization.schema.d.ts.map +1 -1
- package/dist/types/schemas/plans.schema.d.ts +6 -8
- package/dist/types/schemas/plans.schema.d.ts.map +1 -1
- package/dist/types/schemas/project.schema.d.ts +5 -17
- package/dist/types/schemas/project.schema.d.ts.map +1 -1
- package/dist/types/schemas/session.schema.d.ts +14 -0
- package/dist/types/schemas/session.schema.d.ts.map +1 -0
- package/dist/types/schemas/tag.schema.d.ts +6 -8
- package/dist/types/schemas/tag.schema.d.ts.map +1 -1
- package/dist/types/schemas/user.schema.d.ts +7 -5
- package/dist/types/schemas/user.schema.d.ts.map +1 -1
- package/dist/types/services/dictionary.service.d.ts +9 -9
- package/dist/types/services/dictionary.service.d.ts.map +1 -1
- package/dist/types/services/email.service.d.ts +4 -0
- package/dist/types/services/email.service.d.ts.map +1 -1
- package/dist/types/services/oAuth2.service.d.ts +23 -14
- package/dist/types/services/oAuth2.service.d.ts.map +1 -1
- package/dist/types/services/organization.service.d.ts +6 -12
- package/dist/types/services/organization.service.d.ts.map +1 -1
- package/dist/types/services/project.service.d.ts +5 -5
- package/dist/types/services/project.service.d.ts.map +1 -1
- package/dist/types/services/projectAccessKey.service.d.ts +5 -5
- package/dist/types/services/projectAccessKey.service.d.ts.map +1 -1
- package/dist/types/services/subscription.service.d.ts +1 -1
- package/dist/types/services/subscription.service.d.ts.map +1 -1
- package/dist/types/services/tag.service.d.ts +6 -6
- package/dist/types/services/tag.service.d.ts.map +1 -1
- package/dist/types/services/user.service.d.ts +7 -21
- package/dist/types/services/user.service.d.ts.map +1 -1
- package/dist/types/types/dictionary.types.d.ts +11 -9
- package/dist/types/types/dictionary.types.d.ts.map +1 -1
- package/dist/types/types/discussion.types.d.ts +5 -2
- package/dist/types/types/discussion.types.d.ts.map +1 -1
- package/dist/types/types/oAuth2.types.d.ts +5 -2
- package/dist/types/types/oAuth2.types.d.ts.map +1 -1
- package/dist/types/types/organization.types.d.ts +11 -8
- package/dist/types/types/organization.types.d.ts.map +1 -1
- package/dist/types/types/plan.types.d.ts +6 -3
- package/dist/types/types/plan.types.d.ts.map +1 -1
- package/dist/types/types/project.types.d.ts +25 -25
- package/dist/types/types/project.types.d.ts.map +1 -1
- package/dist/types/types/session.types.d.ts +31 -17
- package/dist/types/types/session.types.d.ts.map +1 -1
- package/dist/types/types/tag.types.d.ts +8 -6
- package/dist/types/types/tag.types.d.ts.map +1 -1
- package/dist/types/types/user.types.d.ts +14 -21
- package/dist/types/types/user.types.d.ts.map +1 -1
- package/dist/types/utils/AI/aiSdk.d.ts +2 -2
- package/dist/types/utils/AI/aiSdk.d.ts.map +1 -1
- package/dist/types/utils/AI/askDocQuestion/askDocQuestion.d.ts +2 -0
- package/dist/types/utils/AI/askDocQuestion/askDocQuestion.d.ts.map +1 -1
- package/dist/types/utils/AI/autocomplete/index.d.ts +4 -1
- package/dist/types/utils/AI/autocomplete/index.d.ts.map +1 -1
- package/dist/types/utils/access.d.ts +1 -0
- package/dist/types/utils/access.d.ts.map +1 -0
- package/dist/types/utils/accessControl.d.ts +9 -9
- package/dist/types/utils/accessControl.d.ts.map +1 -1
- package/dist/types/utils/auth/getAuth.d.ts +7 -0
- package/dist/types/utils/auth/getAuth.d.ts.map +1 -0
- package/dist/types/utils/cors.d.ts +3 -0
- package/dist/types/utils/cors.d.ts.map +1 -0
- package/dist/types/utils/ensureMongoDocumentToObject.d.ts +2 -2
- package/dist/types/utils/ensureMongoDocumentToObject.d.ts.map +1 -1
- package/dist/types/utils/errors/ErrorHandler.d.ts +1 -1
- package/dist/types/utils/errors/ErrorHandler.d.ts.map +1 -1
- package/dist/types/utils/errors/errorCodes.d.ts +57 -96
- package/dist/types/utils/errors/errorCodes.d.ts.map +1 -1
- package/dist/types/utils/filtersAndPagination/getOrganizationFiltersAndPagination.d.ts +1 -1
- package/dist/types/utils/filtersAndPagination/getOrganizationFiltersAndPagination.d.ts.map +1 -1
- package/dist/types/utils/filtersAndPagination/getProjectFiltersAndPagination.d.ts +1 -1
- package/dist/types/utils/filtersAndPagination/getProjectFiltersAndPagination.d.ts.map +1 -1
- package/dist/types/utils/filtersAndPagination/getTagFiltersAndPagination.d.ts +1 -1
- package/dist/types/utils/filtersAndPagination/getTagFiltersAndPagination.d.ts.map +1 -1
- package/dist/types/utils/filtersAndPagination/getUserFiltersAndPagination.d.ts +1 -1
- package/dist/types/utils/filtersAndPagination/getUserFiltersAndPagination.d.ts.map +1 -1
- package/dist/types/utils/mapper/dictionary.d.ts +1 -1
- package/dist/types/utils/mapper/dictionary.d.ts.map +1 -1
- package/dist/types/utils/mapper/organization.d.ts +3 -2
- package/dist/types/utils/mapper/organization.d.ts.map +1 -1
- package/dist/types/utils/mapper/project.d.ts +4 -5
- package/dist/types/utils/mapper/project.d.ts.map +1 -1
- package/dist/types/utils/mapper/tag.d.ts +1 -1
- package/dist/types/utils/mapper/tag.d.ts.map +1 -1
- package/dist/types/utils/mapper/user.d.ts +2 -2
- package/dist/types/utils/mapper/user.d.ts.map +1 -1
- package/dist/types/utils/mergeFunctionTypes.d.ts +18 -0
- package/dist/types/utils/mergeFunctionTypes.d.ts.map +1 -0
- package/dist/types/utils/mongoDB/connectDB.d.ts +1 -1
- package/dist/types/utils/mongoDB/connectDB.d.ts.map +1 -1
- package/dist/types/utils/mongoDB/types.d.ts +11 -0
- package/dist/types/utils/mongoDB/types.d.ts.map +1 -0
- package/dist/types/utils/permissions.d.ts +115 -0
- package/dist/types/utils/permissions.d.ts.map +1 -0
- package/dist/types/utils/rateLimiter.d.ts +4 -0
- package/dist/types/utils/rateLimiter.d.ts.map +1 -0
- package/package.json +16 -14
- package/dist/cjs/controllers/sessionAuth.controller.cjs +0 -839
- package/dist/cjs/controllers/sessionAuth.controller.cjs.map +0 -1
- package/dist/cjs/routes/sessionAuth.routes.cjs +0 -154
- package/dist/cjs/routes/sessionAuth.routes.cjs.map +0 -1
- package/dist/cjs/services/sessionAuth.service.cjs +0 -385
- package/dist/cjs/services/sessionAuth.service.cjs.map +0 -1
- package/dist/cjs/utils/CSRF.cjs +0 -50
- package/dist/cjs/utils/CSRF.cjs.map +0 -1
- package/dist/cjs/utils/cookies.cjs +0 -59
- package/dist/cjs/utils/cookies.cjs.map +0 -1
- package/dist/esm/controllers/sessionAuth.controller.mjs +0 -790
- package/dist/esm/controllers/sessionAuth.controller.mjs.map +0 -1
- package/dist/esm/routes/sessionAuth.routes.mjs +0 -142
- package/dist/esm/routes/sessionAuth.routes.mjs.map +0 -1
- package/dist/esm/services/sessionAuth.service.mjs +0 -337
- package/dist/esm/services/sessionAuth.service.mjs.map +0 -1
- package/dist/esm/utils/CSRF.mjs +0 -24
- package/dist/esm/utils/CSRF.mjs.map +0 -1
- package/dist/esm/utils/cookies.mjs +0 -32
- package/dist/esm/utils/cookies.mjs.map +0 -1
- package/dist/types/controllers/sessionAuth.controller.d.ts +0 -140
- package/dist/types/controllers/sessionAuth.controller.d.ts.map +0 -1
- package/dist/types/routes/sessionAuth.routes.d.ts +0 -77
- package/dist/types/routes/sessionAuth.routes.d.ts.map +0 -1
- package/dist/types/services/sessionAuth.service.d.ts +0 -141
- package/dist/types/services/sessionAuth.service.d.ts.map +0 -1
- package/dist/types/utils/CSRF.d.ts +0 -3
- package/dist/types/utils/CSRF.d.ts.map +0 -1
- package/dist/types/utils/cookies.d.ts +0 -12
- package/dist/types/utils/cookies.d.ts.map +0 -1
|
@@ -3,9 +3,7 @@ import { GenericError } from "./../utils/errors/index.mjs";
|
|
|
3
3
|
import {
|
|
4
4
|
validateOrganization
|
|
5
5
|
} from "./../utils/validation/validateOrganization.mjs";
|
|
6
|
-
const findOrganizations = async (filters, skip, limit) =>
|
|
7
|
-
return await OrganizationModel.find(filters).skip(skip).limit(limit);
|
|
8
|
-
};
|
|
6
|
+
const findOrganizations = async (filters, skip, limit) => await OrganizationModel.find(filters).skip(skip).limit(limit);
|
|
9
7
|
const getOrganizationById = async (organizationId) => {
|
|
10
8
|
const organization = await OrganizationModel.findById(organizationId);
|
|
11
9
|
if (!organization) {
|
|
@@ -13,12 +11,6 @@ const getOrganizationById = async (organizationId) => {
|
|
|
13
11
|
}
|
|
14
12
|
return organization;
|
|
15
13
|
};
|
|
16
|
-
const getOrganizationsByOwner = async (userId) => {
|
|
17
|
-
const organization = await OrganizationModel.find({
|
|
18
|
-
creatorId: userId
|
|
19
|
-
});
|
|
20
|
-
return organization;
|
|
21
|
-
};
|
|
22
14
|
const countOrganizations = async (filters) => {
|
|
23
15
|
const result = await OrganizationModel.countDocuments(filters);
|
|
24
16
|
if (typeof result === "undefined") {
|
|
@@ -32,6 +24,14 @@ const createOrganization = async (organization, userId) => {
|
|
|
32
24
|
throw new GenericError("ORGANIZATION_INVALID_FIELDS", { errors });
|
|
33
25
|
}
|
|
34
26
|
try {
|
|
27
|
+
console.log({
|
|
28
|
+
organization: {
|
|
29
|
+
creatorId: userId,
|
|
30
|
+
membersIds: [userId],
|
|
31
|
+
adminsIds: [userId],
|
|
32
|
+
...organization
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
35
|
const result = await OrganizationModel.create({
|
|
36
36
|
creatorId: userId,
|
|
37
37
|
membersIds: [userId],
|
|
@@ -40,7 +40,10 @@ const createOrganization = async (organization, userId) => {
|
|
|
40
40
|
});
|
|
41
41
|
return result;
|
|
42
42
|
} catch (error) {
|
|
43
|
-
|
|
43
|
+
console.error("Organization creation failed:", error);
|
|
44
|
+
throw new GenericError("ORGANIZATION_CREATION_FAILED", {
|
|
45
|
+
error: error.message
|
|
46
|
+
});
|
|
44
47
|
}
|
|
45
48
|
};
|
|
46
49
|
const updateOrganizationById = async (organizationId, organization) => {
|
|
@@ -74,16 +77,16 @@ const updatePlan = async (organization, plan) => {
|
|
|
74
77
|
prevPlan = prevPlan.toObject();
|
|
75
78
|
}
|
|
76
79
|
const updateOrganizationResult = await OrganizationModel.updateOne(
|
|
77
|
-
{ _id: organization.
|
|
80
|
+
{ _id: organization.id },
|
|
78
81
|
{ $set: { plan: { ...prevPlan, ...plan } } },
|
|
79
82
|
{ new: true }
|
|
80
83
|
);
|
|
81
84
|
if (updateOrganizationResult.matchedCount === 0) {
|
|
82
85
|
throw new GenericError("ORGANIZATION_UPDATE_FAILED", {
|
|
83
|
-
organizationId: organization.
|
|
86
|
+
organizationId: organization.id
|
|
84
87
|
});
|
|
85
88
|
}
|
|
86
|
-
const updatedOrganization = await getOrganizationById(organization.
|
|
89
|
+
const updatedOrganization = await getOrganizationById(organization.id);
|
|
87
90
|
return updatedOrganization;
|
|
88
91
|
};
|
|
89
92
|
export {
|
|
@@ -92,7 +95,6 @@ export {
|
|
|
92
95
|
deleteOrganizationById,
|
|
93
96
|
findOrganizations,
|
|
94
97
|
getOrganizationById,
|
|
95
|
-
getOrganizationsByOwner,
|
|
96
98
|
updateOrganizationById,
|
|
97
99
|
updatePlan
|
|
98
100
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/organization.service.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/services/organization.service.ts"],"sourcesContent":["import type {\n Organization,\n OrganizationCreationData,\n OrganizationDocument,\n} from '@/types/organization.types';\nimport type { Plan, PlanDocument } from '@/types/plan.types';\nimport { OrganizationModel } from '@models/organization.model';\nimport { GenericError } from '@utils/errors';\nimport type { OrganizationFilters } from '@utils/filtersAndPagination/getOrganizationFiltersAndPagination';\nimport {\n type OrganizationFields,\n validateOrganization,\n} from '@utils/validation/validateOrganization';\nimport type { Types } from 'mongoose';\n\n/**\n * Finds organizations based on filters and pagination options.\n * @param filters - MongoDB filter query.\n * @param skip - Number of documents to skip.\n * @param limit - Number of documents to limit.\n * @returns List of organizations matching the filters.\n */\nexport const findOrganizations = async (\n filters: OrganizationFilters,\n skip: number,\n limit: number\n): Promise<OrganizationDocument[]> =>\n await OrganizationModel.find(filters).skip(skip).limit(limit);\n\n/**\n * Finds an organization by its ID.\n * @param organizationId - The ID of the organization to find.\n * @returns The organization matching the ID.\n */\nexport const getOrganizationById = async (\n organizationId: string | Types.ObjectId\n): Promise<OrganizationDocument> => {\n const organization = await OrganizationModel.findById(organizationId);\n\n if (!organization) {\n throw new GenericError('ORGANIZATION_NOT_FOUND', { organizationId });\n }\n\n return organization;\n};\n\n/**\n * Counts the total number of organizations that match the filters.\n * @param filters - MongoDB filter query.\n * @returns Total number of organizations.\n */\nexport const countOrganizations = async (\n filters: OrganizationFilters\n): Promise<number> => {\n const result = await OrganizationModel.countDocuments(filters);\n\n if (typeof result === 'undefined') {\n throw new GenericError('ORGANIZATION_COUNT_FAILED', { filters });\n }\n\n return result;\n};\n\n/**\n * Creates a new organization in the database.\n * @param organization - The organization data to create.\n * @returns The created organization.\n */\nexport const createOrganization = async (\n organization: OrganizationCreationData,\n userId: string | Types.ObjectId\n): Promise<OrganizationDocument> => {\n const errors = validateOrganization(organization, ['name']);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('ORGANIZATION_INVALID_FIELDS', { errors });\n }\n\n try {\n console.log({\n organization: {\n creatorId: userId,\n membersIds: [userId],\n adminsIds: [userId],\n ...organization,\n },\n });\n\n const result = await OrganizationModel.create({\n creatorId: userId,\n membersIds: [userId],\n adminsIds: [userId],\n ...organization,\n });\n\n return result;\n } catch (error) {\n console.error('Organization creation failed:', error);\n throw new GenericError('ORGANIZATION_CREATION_FAILED', {\n error: (error as Error).message,\n });\n }\n};\n\n/**\n * Updates an existing organization in the database by its ID.\n * @param organizationId - The ID of the organization to update.\n * @param organization - The updated organization data.\n * @returns The updated organization.\n */\nexport const updateOrganizationById = async (\n organizationId: string | Types.ObjectId,\n organization: Partial<Organization>\n): Promise<OrganizationDocument> => {\n const updatedKeys = Object.keys(organization) as OrganizationFields;\n const errors = validateOrganization(organization, updatedKeys);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('ORGANIZATION_INVALID_FIELDS', {\n organizationId,\n errors,\n });\n }\n\n const result = await OrganizationModel.updateOne(\n { _id: organizationId },\n organization\n );\n\n if (result.matchedCount === 0) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', { organizationId });\n }\n\n return await getOrganizationById(organizationId);\n};\n\n/**\n * Deletes an organization from the database by its ID.\n * @param organizationId - The ID of the organization to delete.\n * @returns The result of the deletion operation.\n */\nexport const deleteOrganizationById = async (\n organizationId: string | Types.ObjectId\n): Promise<OrganizationDocument> => {\n const organization =\n await OrganizationModel.findByIdAndDelete(organizationId);\n\n if (!organization) {\n throw new GenericError('ORGANIZATION_NOT_FOUND', { organizationId });\n }\n\n return organization;\n};\n\n/**\n * Updates an existing plan in the database by its ID.\n * @param planId - The ID of the plan to update.\n * @param plan - The updated plan data.\n * @returns The updated plan.\n */\nexport const updatePlan = async (\n organization: Organization | OrganizationDocument,\n plan: Partial<Plan>\n): Promise<OrganizationDocument | null> => {\n let prevPlan = organization.plan ?? {};\n\n if (typeof (prevPlan as PlanDocument)?.toObject === 'function') {\n prevPlan = (prevPlan as PlanDocument).toObject();\n }\n\n const updateOrganizationResult = await OrganizationModel.updateOne(\n { _id: organization.id },\n { $set: { plan: { ...prevPlan, ...plan } } },\n { new: true }\n );\n\n if (updateOrganizationResult.matchedCount === 0) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization.id,\n });\n }\n\n const updatedOrganization = await getO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
|
|
@@ -33,7 +33,7 @@ const createProject = async (project) => {
|
|
|
33
33
|
const updateProjectById = async (projectId, project) => {
|
|
34
34
|
const projectObject = ensureMongoDocumentToObject(project);
|
|
35
35
|
const projectToUpdate = removeObjectKeys(projectObject, [
|
|
36
|
-
"
|
|
36
|
+
"id",
|
|
37
37
|
"oAuth2Access",
|
|
38
38
|
"organizationId"
|
|
39
39
|
]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/project.service.ts"],"sourcesContent":["import { ProjectModel } from '@models/project.model';\nimport { ensureMongoDocumentToObject } from '@utils/ensureMongoDocumentToObject';\nimport { GenericError } from '@utils/errors';\nimport type { ProjectFilters } from '@utils/filtersAndPagination/getProjectFiltersAndPagination';\nimport { removeObjectKeys } from '@utils/removeObjectKeys';\nimport {\n type ProjectFields,\n validateProject,\n} from '@utils/validation/validateProject';\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../../src/services/project.service.ts"],"sourcesContent":["import type {\n Project,\n ProjectData,\n ProjectDocument,\n} from '@/types/project.types';\nimport { ProjectModel } from '@models/project.model';\nimport { ensureMongoDocumentToObject } from '@utils/ensureMongoDocumentToObject';\nimport { GenericError } from '@utils/errors';\nimport type { ProjectFilters } from '@utils/filtersAndPagination/getProjectFiltersAndPagination';\nimport { removeObjectKeys } from '@utils/removeObjectKeys';\nimport {\n type ProjectFields,\n validateProject,\n} from '@utils/validation/validateProject';\nimport type { Types } from 'mongoose';\n\n/**\n * Finds projects based on filters and pagination options.\n * @param filters - MongoDB filter query.\n * @param skip - Number of documents to skip.\n * @param limit - Number of documents to limit.\n * @returns List of projects matching the filters.\n */\nexport const findProjects = async (\n filters: ProjectFilters,\n skip = 0,\n limit = 100\n): Promise<ProjectDocument[]> =>\n await ProjectModel.find(filters).skip(skip).limit(limit);\n\n/**\n * Finds a project by its ID.\n * @param projectId - The ID of the project to find.\n * @returns The project matching the ID.\n */\nexport const getProjectById = async (\n projectId: string | Types.ObjectId\n): Promise<ProjectDocument> => {\n const project = await ProjectModel.findById(projectId);\n\n if (!project) {\n throw new GenericError('PROJECT_NOT_DEFINED', { projectId });\n }\n\n return project;\n};\n\n/**\n * Counts the total number of projects that match the filters.\n * @param filters - MongoDB filter query.\n * @returns Total number of projects.\n */\nexport const countProjects = async (\n filters: ProjectFilters\n): Promise<number> => {\n const result = await ProjectModel.countDocuments(filters);\n\n if (typeof result === 'undefined') {\n throw new GenericError('PROJECT_COUNT_FAILED', { filters });\n }\n\n return result;\n};\n\n/**\n * Creates a new project in the database.\n * @param project - The project data to create.\n * @returns The created project.\n */\nexport const createProject = async (\n project: ProjectData\n): Promise<ProjectDocument> => {\n if ((project as Partial<Project>).oAuth2Access) {\n delete (project as Partial<Project>).oAuth2Access;\n }\n\n const errors = await validateProject(project, ['name']);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('PROJECT_INVALID_FIELDS', { errors });\n }\n\n return await ProjectModel.create(project);\n};\n\n/**\n * Updates an existing project in the database by its ID.\n * @param projectId - The ID of the project to update.\n * @param project - The updated project data.\n * @returns The updated project.\n */\nexport const updateProjectById = async (\n projectId: string | Types.ObjectId,\n project: Partial<Project>\n): Promise<ProjectDocument> => {\n const projectObject = ensureMongoDocumentToObject(project);\n const projectToUpdate = removeObjectKeys(projectObject, [\n 'id',\n 'oAuth2Access',\n 'organizationId',\n ]);\n\n const updatedKeys = Object.keys(projectToUpdate) as ProjectFields;\n\n const errors = validateProject(project, updatedKeys);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('PROJECT_INVALID_FIELDS', {\n projectId,\n errors,\n });\n }\n\n const result = await ProjectModel.updateOne(\n { _id: projectId },\n projectToUpdate\n );\n\n if (result.matchedCount === 0) {\n throw new GenericError('PROJECT_UPDATE_FAILED', { projectId });\n }\n\n return await getProjectById(projectId);\n};\n\n/**\n * Deletes a project from the database by its ID.\n * @param projectId - The ID of the project to delete.\n * @returns The result of the deletion operation.\n */\nexport const deleteProjectById = async (\n projectId: string | Types.ObjectId\n): Promise<ProjectDocument> => {\n const project = await ProjectModel.findByIdAndDelete(projectId);\n\n if (!project) {\n throw new GenericError('PROJECT_NOT_DEFINED', { projectId });\n }\n\n return project;\n};\n"],"mappings":"AAKA,SAAS,oBAAoB;AAC7B,SAAS,mCAAmC;AAC5C,SAAS,oBAAoB;AAE7B,SAAS,wBAAwB;AACjC;AAAA,EAEE;AAAA,OACK;AAUA,MAAM,eAAe,OAC1B,SACA,OAAO,GACP,QAAQ,QAER,MAAM,aAAa,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,KAAK;AAOlD,MAAM,iBAAiB,OAC5B,cAC6B;AAC7B,QAAM,UAAU,MAAM,aAAa,SAAS,SAAS;AAErD,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,aAAa,uBAAuB,EAAE,UAAU,CAAC;AAAA,EAC7D;AAEA,SAAO;AACT;AAOO,MAAM,gBAAgB,OAC3B,YACoB;AACpB,QAAM,SAAS,MAAM,aAAa,eAAe,OAAO;AAExD,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,aAAa,wBAAwB,EAAE,QAAQ,CAAC;AAAA,EAC5D;AAEA,SAAO;AACT;AAOO,MAAM,gBAAgB,OAC3B,YAC6B;AAC7B,MAAK,QAA6B,cAAc;AAC9C,WAAQ,QAA6B;AAAA,EACvC;AAEA,QAAM,SAAS,MAAM,gBAAgB,SAAS,CAAC,MAAM,CAAC;AAEtD,MAAI,OAAO,KAAK,MAAM,EAAE,SAAS,GAAG;AAClC,UAAM,IAAI,aAAa,0BAA0B,EAAE,OAAO,CAAC;AAAA,EAC7D;AAEA,SAAO,MAAM,aAAa,OAAO,OAAO;AAC1C;AAQO,MAAM,oBAAoB,OAC/B,WACA,YAC6B;AAC7B,QAAM,gBAAgB,4BAA4B,OAAO;AACzD,QAAM,kBAAkB,iBAAiB,eAAe;AAAA,IACtD;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,cAAc,OAAO,KAAK,eAAe;AAE/C,QAAM,SAAS,gBAAgB,SAAS,WAAW;AAEnD,MAAI,OAAO,KAAK,MAAM,EAAE,SAAS,GAAG;AAClC,UAAM,IAAI,aAAa,0BAA0B;AAAA,MAC/C;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,SAAS,MAAM,aAAa;AAAA,IAChC,EAAE,KAAK,UAAU;AAAA,IACjB;AAAA,EACF;AAEA,MAAI,OAAO,iBAAiB,GAAG;AAC7B,UAAM,IAAI,aAAa,yBAAyB,EAAE,UAAU,CAAC;AAAA,EAC/D;AAEA,SAAO,MAAM,eAAe,SAAS;AACvC;AAOO,MAAM,oBAAoB,OAC/B,cAC6B;AAC7B,QAAM,UAAU,MAAM,aAAa,kBAAkB,SAAS;AAE9D,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,aAAa,uBAAuB,EAAE,UAAU,CAAC;AAAA,EAC7D;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import { ProjectModel } from "./../models/project.model.mjs";
|
|
2
2
|
import { GenericError } from "./../utils/errors/index.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { randomBytes } from "crypto";
|
|
4
4
|
import { getProjectById } from "./project.service.mjs";
|
|
5
|
-
const
|
|
5
|
+
const generateClientCredentials = () => ({
|
|
6
|
+
clientId: randomBytes(16).toString("hex"),
|
|
7
|
+
// 32 character hexadecimal string
|
|
8
|
+
clientSecret: randomBytes(32).toString("hex")
|
|
9
|
+
// 64 character hexadecimal string
|
|
10
|
+
});
|
|
11
|
+
const addNewAccessKey = async (accessKeyData, projectId, user) => {
|
|
6
12
|
const { clientId, clientSecret } = generateClientCredentials();
|
|
7
13
|
const newAccessKey = {
|
|
8
14
|
...accessKeyData,
|
|
9
15
|
clientId,
|
|
10
16
|
clientSecret,
|
|
11
|
-
userId: user.
|
|
17
|
+
userId: user.id,
|
|
12
18
|
accessToken: [],
|
|
13
|
-
|
|
14
|
-
accessKeyData,
|
|
15
|
-
organizationRights,
|
|
16
|
-
projectRights,
|
|
17
|
-
dictionaryRights
|
|
18
|
-
)
|
|
19
|
+
grants: accessKeyData.grants
|
|
19
20
|
};
|
|
20
21
|
const result = await ProjectModel.updateOne(
|
|
21
22
|
{ _id: projectId },
|
|
@@ -25,7 +26,7 @@ const addNewAccessKey = async (accessKeyData, projectId, user, organizationRight
|
|
|
25
26
|
throw new GenericError("ACCESS_KEY_CREATION_FAILED", {
|
|
26
27
|
accessKeyData,
|
|
27
28
|
projectId,
|
|
28
|
-
userId: user.
|
|
29
|
+
userId: user.id
|
|
29
30
|
});
|
|
30
31
|
}
|
|
31
32
|
const updatedProject = await getProjectById(projectId);
|
|
@@ -36,7 +37,7 @@ const addNewAccessKey = async (accessKeyData, projectId, user, organizationRight
|
|
|
36
37
|
throw new GenericError("ACCESS_KEY_CREATION_FAILED", {
|
|
37
38
|
accessKeyData,
|
|
38
39
|
projectId,
|
|
39
|
-
userId: user.
|
|
40
|
+
userId: user.id
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
43
|
return newAccessKeyId;
|
|
@@ -48,7 +49,7 @@ const deleteAccessKey = async (clientId, project, userId) => {
|
|
|
48
49
|
if (!projectAccess) {
|
|
49
50
|
throw new GenericError("ACCESS_KEY_NOT_FOUND", {
|
|
50
51
|
clientId,
|
|
51
|
-
projectId: project.
|
|
52
|
+
projectId: project.id
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
const result = await ProjectModel.updateOne(
|
|
@@ -61,7 +62,7 @@ const deleteAccessKey = async (clientId, project, userId) => {
|
|
|
61
62
|
if (result.modifiedCount === 0) {
|
|
62
63
|
throw new GenericError("ACCESS_KEY_DELETION_FAILED", {
|
|
63
64
|
clientId,
|
|
64
|
-
projectId: project.
|
|
65
|
+
projectId: project.id
|
|
65
66
|
});
|
|
66
67
|
}
|
|
67
68
|
return projectAccess;
|
|
@@ -85,7 +86,7 @@ const refreshAccessKey = async (clientId, projectId, userId) => {
|
|
|
85
86
|
if (!projectAccess) {
|
|
86
87
|
throw new GenericError("ACCESS_KEY_NOT_FOUND", {
|
|
87
88
|
clientId,
|
|
88
|
-
projectId: project.
|
|
89
|
+
projectId: project.id
|
|
89
90
|
});
|
|
90
91
|
}
|
|
91
92
|
const { clientSecret } = generateClientCredentials();
|
|
@@ -120,23 +121,6 @@ const refreshAccessKey = async (clientId, projectId, userId) => {
|
|
|
120
121
|
}
|
|
121
122
|
return newAccessKeyId;
|
|
122
123
|
};
|
|
123
|
-
const restrictRights = (givenRights, userRights) => {
|
|
124
|
-
const restrictedRights = {};
|
|
125
|
-
for (const key in givenRights) {
|
|
126
|
-
if (Object.prototype.hasOwnProperty.call(givenRights, key)) {
|
|
127
|
-
restrictedRights[key] = givenRights[key] && userRights[key];
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return restrictedRights;
|
|
131
|
-
};
|
|
132
|
-
const restrictAccessKeyRights = (accessKey, organizationsRights, projectRights, dictionaryRights) => ({
|
|
133
|
-
dictionary: restrictRights(accessKey.rights.dictionary, dictionaryRights),
|
|
134
|
-
project: restrictRights(accessKey.rights.project, projectRights),
|
|
135
|
-
organization: restrictRights(
|
|
136
|
-
accessKey.rights.organization,
|
|
137
|
-
organizationsRights
|
|
138
|
-
)
|
|
139
|
-
});
|
|
140
124
|
export {
|
|
141
125
|
addNewAccessKey,
|
|
142
126
|
deleteAccessKey,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/projectAccessKey.service.ts"],"sourcesContent":["import { ProjectModel } from '@models/project.model';\nimport { GenericError } from '@utils/errors';\nimport
|
|
1
|
+
{"version":3,"sources":["../../../src/services/projectAccessKey.service.ts"],"sourcesContent":["import type {\n AccessKeyData,\n OAuth2Access,\n OAuth2AccessData,\n Project,\n} from '@/types/project.types';\nimport type { User } from '@/types/user.types';\nimport { ProjectModel } from '@models/project.model';\nimport { GenericError } from '@utils/errors';\nimport { randomBytes } from 'crypto';\nimport type { Types } from 'mongoose';\nimport { getProjectById } from './project.service';\n\n/**\n * Generates cryptographically secure OAuth2 client credentials\n *\n * @returns Object containing clientId and clientSecret\n *\n * Security improvements:\n * - clientId: 32 characters (128 bits of entropy)\n * - clientSecret: 64 characters (256 bits of entropy)\n * - Uses crypto.randomBytes for cryptographically secure random generation\n * - Follows OAuth2 best practices for credential strength\n */\nconst generateClientCredentials = () => ({\n clientId: randomBytes(16).toString('hex'), // 32 character hexadecimal string\n clientSecret: randomBytes(32).toString('hex'), // 64 character hexadecimal string\n});\n\n/**\n * Adds a new access key to a project.\n *\n * @param accessKeyData - The access key data.\n * @param projectId - The ID of the project to add the access key to.\n * @param user - The user adding the access key.\n * @returns The new access key.\n *\n */\nexport const addNewAccessKey = async (\n accessKeyData: AccessKeyData,\n projectId: string | Types.ObjectId,\n user: User\n): Promise<OAuth2Access> => {\n const { clientId, clientSecret } = generateClientCredentials();\n\n const newAccessKey: OAuth2AccessData = {\n ...accessKeyData,\n clientId,\n clientSecret,\n userId: user.id,\n accessToken: [],\n grants: accessKeyData.grants,\n };\n\n const result = await ProjectModel.updateOne(\n { _id: projectId },\n { $push: { oAuth2Access: newAccessKey } }\n );\n\n if (result.modifiedCount === 0) {\n throw new GenericError('ACCESS_KEY_CREATION_FAILED', {\n accessKeyData,\n projectId,\n userId: user.id,\n });\n }\n\n const updatedProject = await getProjectById(projectId);\n\n const newAccessKeyId = updatedProject.oAuth2Access.find(\n (access) => access.clientId === clientId\n );\n\n if (!newAccessKeyId) {\n throw new GenericError('ACCESS_KEY_CREATION_FAILED', {\n accessKeyData,\n projectId,\n userId: user.id,\n });\n }\n\n return newAccessKeyId;\n};\n\nexport const deleteAccessKey = async (\n clientId: string | Types.ObjectId,\n project: Project,\n userId: string | Types.ObjectId\n) => {\n const projectAccess = project.oAuth2Access.find(\n (access) =>\n access.clientId === clientId && String(access.userId) === String(userId)\n );\n\n if (!projectAccess) {\n throw new GenericError('ACCESS_KEY_NOT_FOUND', {\n clientId,\n projectId: project.id,\n });\n }\n\n const result = await ProjectModel.updateOne(\n {\n 'oAuth2Access.clientId': clientId,\n 'oAuth2Access.userId': String(userId),\n },\n { $pull: { oAuth2Access: { clientId } } }\n );\n\n if (result.modifiedCount === 0) {\n throw new GenericError('ACCESS_KEY_DELETION_FAILED', {\n clientId,\n projectId: project.id,\n });\n }\n\n return projectAccess;\n};\n\nexport const refreshAccessKey = async (\n clientId: string | Types.ObjectId,\n projectId: string | Types.ObjectId,\n userId: string | Types.ObjectId\n): Promise<OAuth2Access> => {\n const project = await ProjectModel.findOne({\n _id: projectId,\n 'oAuth2Access.clientId': clientId,\n 'oAuth2Access.userId': String(userId),\n });\n\n if (!project) {\n throw new GenericError('PROJECT_NOT_FOUND', {\n clientId,\n projectId,\n userId,\n });\n }\n\n const projectAccess = project.oAuth2Access.find(\n (access) => access.clientId === clientId\n );\n\n if (!projectAccess) {\n throw new GenericError('ACCESS_KEY_NOT_FOUND', {\n clientId,\n projectId: project.id,\n });\n }\n\n const { clientSecret } = generateClientCredentials();\n\n const result = await ProjectModel.updateOne(\n {\n 'oAuth2Access.clientId': clientId,\n 'oAuth2Access.userId': String(userId),\n },\n {\n $set: {\n 'oAuth2Access.$.clientId': projectAccess.clientId,\n 'oAuth2Access.$.clientSecret': clientSecret,\n },\n }\n );\n\n if (result.modifiedCount === 0) {\n throw new GenericError('ACCESS_KEY_UPDATE_FAILED', {\n clientId,\n projectId,\n });\n }\n\n const updatedProject = await getProjectById(projectId);\n\n const newAccessKeyId = updatedProject.oAuth2Access.find(\n (access) => access.clientId === projectAccess.clientId\n );\n\n if (!newAccessKeyId) {\n throw new GenericError('ACCESS_KEY_CREATION_FAILED', {\n accessKeyData: updatedProject.oAuth2Access,\n projectId,\n userId,\n });\n }\n\n return newAccessKeyId;\n};\n"],"mappings":"AAOA,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAE5B,SAAS,sBAAsB;AAa/B,MAAM,4BAA4B,OAAO;AAAA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c;AAAA,EAChD;AAEA,MAAI,CAAC,gBAAgB;AACnB,UAAM,IAAI,aAAa,8BAA8B;AAAA,MACnD,eAAe,eAAe;AAAA,MAC9B;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -13,7 +13,7 @@ const addOrUpdateSubscription = async (subscriptionId, priceId, customerId, user
|
|
|
13
13
|
userId
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
if (user.customerId !== customerId) {
|
|
16
|
+
if (String(user.customerId) !== customerId) {
|
|
17
17
|
user.customerId = customerId;
|
|
18
18
|
await user.save();
|
|
19
19
|
}
|
|
@@ -32,7 +32,7 @@ const addOrUpdateSubscription = async (subscriptionId, priceId, customerId, user
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
const updatedOrganization = await updatePlan(organization, {
|
|
35
|
-
creatorId: user.
|
|
35
|
+
creatorId: user.id,
|
|
36
36
|
priceId,
|
|
37
37
|
customerId,
|
|
38
38
|
subscriptionId,
|
|
@@ -42,11 +42,11 @@ const addOrUpdateSubscription = async (subscriptionId, priceId, customerId, user
|
|
|
42
42
|
});
|
|
43
43
|
if (!updatedOrganization) {
|
|
44
44
|
throw new GenericError("ORGANIZATION_UPDATE_FAILED", {
|
|
45
|
-
organizationId: organization.
|
|
45
|
+
organizationId: organization.id
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
logger.info(
|
|
49
|
-
`Plan updated for organization ${organization.
|
|
49
|
+
`Plan updated for organization ${organization.id} - ${planInfo.type} - ${planInfo.period}`
|
|
50
50
|
);
|
|
51
51
|
return updatedOrganization.plan ?? null;
|
|
52
52
|
};
|
|
@@ -63,7 +63,7 @@ const cancelSubscription = async (subscriptionId, organizationId) => {
|
|
|
63
63
|
if (!organization.plan) {
|
|
64
64
|
throw new GenericError("ORGANIZATION_PLAN_NOT_FOUND", {
|
|
65
65
|
subscriptionId,
|
|
66
|
-
organizationId: organization.
|
|
66
|
+
organizationId: organization.id
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
const updatedOrganization = await updatePlan(organization, {
|
|
@@ -71,11 +71,11 @@ const cancelSubscription = async (subscriptionId, organizationId) => {
|
|
|
71
71
|
});
|
|
72
72
|
if (!updatedOrganization) {
|
|
73
73
|
throw new GenericError("ORGANIZATION_UPDATE_FAILED", {
|
|
74
|
-
organizationId: organization.
|
|
74
|
+
organizationId: organization.id
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
logger.info(
|
|
78
|
-
`Cancelled plan for organization ${updatedOrganization.
|
|
78
|
+
`Cancelled plan for organization ${updatedOrganization.id} - ${updatedOrganization.plan?.type} - ${updatedOrganization.plan?.period}`
|
|
79
79
|
);
|
|
80
80
|
return updatedOrganization.plan ?? null;
|
|
81
81
|
};
|
|
@@ -91,7 +91,7 @@ const changeSubscriptionStatus = async (subscriptionId, status, userId, organiza
|
|
|
91
91
|
throw new GenericError("ORGANIZATION_PLAN_NOT_FOUND", {
|
|
92
92
|
userId,
|
|
93
93
|
subscriptionId,
|
|
94
|
-
organizationId: organization.
|
|
94
|
+
organizationId: organization.id
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
97
|
const updatedOrganization = await updatePlan(organization, {
|
|
@@ -100,7 +100,7 @@ const changeSubscriptionStatus = async (subscriptionId, status, userId, organiza
|
|
|
100
100
|
});
|
|
101
101
|
if (!updatedOrganization) {
|
|
102
102
|
throw new GenericError("ORGANIZATION_UPDATE_FAILED", {
|
|
103
|
-
organizationId: organization.
|
|
103
|
+
organizationId: organization.id
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
106
|
const user = await getUserById(userId);
|
|
@@ -111,7 +111,7 @@ const changeSubscriptionStatus = async (subscriptionId, status, userId, organiza
|
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
113
|
logger.info(
|
|
114
|
-
`Updated plan status for organization ${organization.
|
|
114
|
+
`Updated plan status for organization ${organization.id} - Status: ${status}`
|
|
115
115
|
);
|
|
116
116
|
const emailData = {
|
|
117
117
|
to: user.email,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/subscription.service.ts"],"sourcesContent":["import { logger } from '@logger';\nimport { GenericError } from '@utils/errors';\nimport { retrievePlanInformation } from '@utils/plan';\nimport Stripe from 'stripe';\nimport { sendEmail } from './email.service';\nimport { getOrganizationById, updatePlan } from './organization.service';\nimport { getUserById } from './user.service';\nimport type { Organization } from '@/types/organization.types';\nimport type { Plan } from '@/types/plan.types';\n\nexport const addOrUpdateSubscription = async (\n subscriptionId: string,\n priceId: string,\n customerId: string,\n userId: string,\n organization: Organization,\n status: Plan['status']\n): Promise<Plan | null> => {\n const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\n const user = await getUserById(userId);\n\n if (!user) {\n throw new GenericError('USER_NOT_FOUND', {\n userId,\n });\n }\n\n if (user.customerId !== customerId) {\n user.customerId = customerId;\n await user.save();\n }\n\n const planInfo = retrievePlanInformation(priceId);\n\n const subscriptions = await stripe.subscriptions.list({\n customer: customerId,\n status: 'active',\n limit: 1,\n });\n\n if (subscriptions.data.length >= 1) {\n // Active subscription exists; update it to the new plan\n const otherSubscriptionArray = subscriptions.data.filter(\n (subscription) => subscription.id !== subscriptionId\n );\n\n for (const subscription of otherSubscriptionArray) {\n await stripe.subscriptions.cancel(subscription.id);\n }\n }\n\n const updatedOrganization = await updatePlan(organization, {\n creatorId: user._id,\n priceId,\n customerId,\n subscriptionId,\n type: planInfo.type,\n period: planInfo.period,\n status,\n });\n\n if (!updatedOrganization) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization._id,\n });\n }\n\n logger.info(\n `Plan updated for organization ${organization._id} - ${planInfo.type} - ${planInfo.period}`\n );\n\n return updatedOrganization.plan ?? null;\n};\n\nexport const cancelSubscription = async (\n subscriptionId: string | Organization['_id'],\n organizationId: Organization['_id'] | string\n): Promise<Plan | null> => {\n const organization = await getOrganizationById(organizationId);\n\n if (!organization) {\n throw new GenericError('ORGANIZATION_NOT_FOUND', {\n subscriptionId,\n });\n }\n\n if (!subscriptionId) {\n throw new GenericError('NO_SUBSCRIPTION_ID_PROVIDED');\n }\n\n if (!organization.plan) {\n throw new GenericError('ORGANIZATION_PLAN_NOT_FOUND', {\n subscriptionId,\n organizationId: organization._id,\n });\n }\n\n const updatedOrganization = await updatePlan(organization, {\n status: 'canceled',\n });\n\n if (!updatedOrganization) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization._id,\n });\n }\n\n logger.info(\n `Cancelled plan for organization ${updatedOrganization._id} - ${updatedOrganization.plan?.type} - ${updatedOrganization.plan?.period}`\n );\n\n return updatedOrganization.plan ?? null;\n};\n\nexport const changeSubscriptionStatus = async (\n subscriptionId: string,\n status: Plan['status'],\n userId: string,\n organizationId: string\n): Promise<Plan | null> => {\n const organization = await getOrganizationById(organizationId);\n\n if (!organization) {\n throw new GenericError('ORGANIZATION_NOT_FOUND', {\n userId,\n subscriptionId,\n });\n }\n\n if (!organization.plan) {\n throw new GenericError('ORGANIZATION_PLAN_NOT_FOUND', {\n userId,\n subscriptionId,\n organizationId: organization._id,\n });\n }\n\n const updatedOrganization = await updatePlan(organization, {\n status,\n subscriptionId,\n });\n\n if (!updatedOrganization) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization._id,\n });\n }\n\n const user = await getUserById(userId);\n\n if (!user) {\n throw new GenericError('USER_NOT_FOUND', {\n userId,\n subscriptionId,\n });\n }\n\n logger.info(\n `Updated plan status for organization ${organization._id} - Status: ${status}`\n );\n\n const emailData = {\n to: user.email,\n username: user.name,\n email: user.email,\n planName: organization.plan.type,\n date: new Date().toLocaleDateString(),\n link: `${process.env.CLIENT_URL}/dashboard`,\n };\n\n switch (status) {\n case 'active':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentSuccess',\n organizationName: organization.name,\n subscriptionStartDate: emailData.date,\n manageSubscriptionLink: emailData.link,\n });\n break;\n case 'canceled':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentCancellation',\n organizationName: organization.name,\n cancellationDate: emailData.date,\n reactivateLink: emailData.link,\n });\n break;\n case 'incomplete':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentError',\n organizationName: organization.name,\n errorDate: emailData.date,\n retryPaymentLink: emailData.link,\n });\n break;\n default:\n logger.warn(`Unhandled subscription status: ${status}`);\n }\n\n return updatedOrganization.plan ?? null;\n};\n\nexport const getCouponId = async (\n promoCode: string\n): Promise<string | null> => {\n const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\n\n try {\n // Retrieve the coupon details by name\n const coupons = await stripe.coupons.list();\n const matchingCoupon = coupons.data.find(\n (coupon) => coupon.name === promoCode\n );\n\n return matchingCoupon ? matchingCoupon.id : null;\n } catch (error) {\n console.error('Error retrieving coupon:', error);\n return null;\n }\n};\n\nexport type PricingResult = Record<\n string,\n {\n originalTotal: number;\n discountApplied: number;\n discountType: 'amount' | 'percentage' | null;\n finalTotal: number;\n currency: string;\n }\n>;\n\nexport const getPricing = async (\n priceIds: string[],\n promoCode?: string\n): Promise<PricingResult> => {\n const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\n\n try {\n // 1. Fetch all price objects\n const pricePromises = priceIds.map((priceId) =>\n stripe.prices.retrieve(priceId)\n );\n const prices = await Promise.all(pricePromises);\n\n // Calculate the total amount before discount (to help with proportional distribution if needed)\n const totalAmount = prices.reduce(\n (sum, price) => sum + (price.unit_amount ?? 0),\n 0\n );\n\n // 2. Retrieve the discount (if promo code is provided)\n let discountAmount = 0;\n let discountType: 'amount' | 'percentage' | null = null;\n\n if (promoCode) {\n const coupons = await stripe.coupons.list();\n const matchingCoupons = coupons.data.find(\n (coupon) => coupon.name === promoCode\n );\n if (matchingCoupons) {\n if (matchingCoupons.amount_off) {\n discountAmount = matchingCoupons.amount_off;\n discountType = 'amount';\n } else if (matchingCoupons.percent_off) {\n // For a percentage discount, we won't store discountAmount as a raw number\n // because each price line is discounted individually by the same percentage.\n discountAmount = matchingCoupons.percent_off;\n discountType = 'percentage';\n }\n }\n }\n\n // 3. Build the result for each priceId\n const results: PricingResult = {};\n\n for (const price of prices) {\n if (!price.id || !price.unit_amount) {\n continue; // Skip any invalid price\n }\n\n const originalTotal = price.unit_amount;\n let appliedDiscount = 0;\n let finalTotal = originalTotal;\n\n // Apply discount based on the discount type\n if (discountType === 'percentage' && discountAmount > 0) {\n // percentage-based discount\n appliedDiscount = (originalTotal * discountAmount) / 100;\n finalTotal = originalTotal - appliedDiscount;\n } else if (\n discountType === 'amount' &&\n totalAmount > 0 &&\n discountAmount > 0\n ) {\n // fixed amount discount - distribute proportionally\n const proportion = originalTotal / totalAmount;\n appliedDiscount = discountAmount * proportion;\n finalTotal = originalTotal - appliedDiscount;\n }\n\n // Prevent final total from going negative due to rounding\n finalTotal = Math.max(finalTotal, 0);\n\n results[price.id] = {\n originalTotal: originalTotal,\n discountApplied: appliedDiscount,\n discountType,\n finalTotal: finalTotal,\n currency: price.currency,\n };\n }\n\n return results;\n } catch (error) {\n console.error('Error calculating pricing per priceId:', error);\n throw new Error('Failed to calculate pricing breakdown.');\n }\n};\n"],"mappings":"AAAA,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAC7B,SAAS,+BAA+B;AACxC,OAAO,YAAY;AACnB,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB,kBAAkB;AAChD,SAAS,mBAAmB;AAIrB,MAAM,0BAA0B,OACrC,gBACA,SACA,YACA,QACA,cACA,WACyB;AACzB,QAAM,SAAS,IAAI,OAAO,QAAQ,IAAI,iBAAkB;AACxD,QAAM,OAAO,MAAM,YAAY,MAAM;AAErC,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,aAAa,kBAAkB;AAAA,MACvC;AAAA,IACF,CAAC;AAAA,EACH;AAEA,MAAI,KAAK,eAAe,YAAY;AAClC,SAAK,aAAa;AAClB,UAAM,KAAK,KAAK;AAAA,EAClB;AAEA,QAAM,WAAW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Ae;AAAA,MAC5C;AAAA,IACF;AAGA,QAAI,iBAAiB;AACrB,QAAI,eAA+C;AAEnD,QAAI,WAAW;AACb,YAAM,UAAU,MAAM,OAAO,QAAQ,KAAK;AAC1C,YAAM,kBAAkB,QAAQ,KAAK;AAAA,QACnC,CAAC,WAAW,OAAO,SAAS;AAAA,MAC9B;AACA,UAAI,iBAAiB;AACnB,YAAI,gBAAgB,YAAY;AAC9B,2BAAiB,gBAAgB;AACjC,yBAAe;AAAA,QACjB,WAAW,gBAAgB,aAAa;AAGtC,2BAAiB,gBAAgB;AACjC,yBAAe;AAAA,QACjB;AAAA,MACF;AAAA,IACF;AAGA,UAAM,UAAyB,CAAC;AAEhC,eAAW,SAAS,QAAQ;AAC1B,UAAI,CAAC,MAAM,MAAM,CAAC,MAAM,aAAa;AACnC;AAAA,MACF;AAEA,YAAM,gBAAgB,MAAM;AAC5B,UAAI,kBAAkB;AACtB,UAAI,aAAa;AAGjB,UAAI,iBAAiB,gBAAgB,iBAAiB,GAAG;AAEvD,0BAAmB,gBAAgB,iBAAkB;AACrD,qBAAa,gBAAgB;AAAA,MAC/B,WACE,iBAAiB,YACjB,cAAc,KACd,iBAAiB,GACjB;AAEA,cAAM,aAAa,gBAAgB;AACnC,0BAAkB,iBAAiB;AACnC,qBAAa,gBAAgB;AAAA,MAC/B;AAGA,mBAAa,KAAK,IAAI,YAAY,CAAC;AAEnC,cAAQ,MAAM,EAAE,IAAI;AAAA,QAClB;AAAA,QACA,iBAAiB;AAAA,QACjB;AAAA,QACA;AAAA,QACA,UAAU,MAAM;AAAA,MAClB;AAAA,IACF;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,0CAA0C,KAAK;AAC7D,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/services/subscription.service.ts"],"sourcesContent":["import type { Organization } from '@/types/organization.types';\nimport type { Plan } from '@/types/plan.types';\nimport { logger } from '@logger';\nimport { GenericError } from '@utils/errors';\nimport { retrievePlanInformation } from '@utils/plan';\nimport Stripe from 'stripe';\nimport { sendEmail } from './email.service';\nimport { getOrganizationById, updatePlan } from './organization.service';\nimport { getUserById } from './user.service';\n\nexport const addOrUpdateSubscription = async (\n subscriptionId: string,\n priceId: string,\n customerId: string,\n userId: string,\n organization: Organization,\n status: Plan['status']\n): Promise<Plan | null> => {\n const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\n const user = await getUserById(userId);\n\n if (!user) {\n throw new GenericError('USER_NOT_FOUND', {\n userId,\n });\n }\n\n if (String(user.customerId) !== customerId) {\n (user.customerId as unknown as string) = customerId;\n await user.save();\n }\n\n const planInfo = retrievePlanInformation(priceId);\n\n const subscriptions = await stripe.subscriptions.list({\n customer: customerId,\n status: 'active',\n limit: 1,\n });\n\n if (subscriptions.data.length >= 1) {\n // Active subscription exists; update it to the new plan\n const otherSubscriptionArray = subscriptions.data.filter(\n (subscription) => subscription.id !== subscriptionId\n );\n\n for (const subscription of otherSubscriptionArray) {\n await stripe.subscriptions.cancel(subscription.id);\n }\n }\n\n const updatedOrganization = await updatePlan(organization, {\n creatorId: user.id,\n priceId,\n customerId,\n subscriptionId,\n type: planInfo.type,\n period: planInfo.period,\n status,\n });\n\n if (!updatedOrganization) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization.id,\n });\n }\n\n logger.info(\n `Plan updated for organization ${organization.id} - ${planInfo.type} - ${planInfo.period}`\n );\n\n return updatedOrganization.plan ?? null;\n};\n\nexport const cancelSubscription = async (\n subscriptionId: string | Organization['id'],\n organizationId: Organization['id'] | string\n): Promise<Plan | null> => {\n const organization = await getOrganizationById(organizationId);\n\n if (!organization) {\n throw new GenericError('ORGANIZATION_NOT_FOUND', {\n subscriptionId,\n });\n }\n\n if (!subscriptionId) {\n throw new GenericError('NO_SUBSCRIPTION_ID_PROVIDED');\n }\n\n if (!organization.plan) {\n throw new GenericError('ORGANIZATION_PLAN_NOT_FOUND', {\n subscriptionId,\n organizationId: organization.id,\n });\n }\n\n const updatedOrganization = await updatePlan(organization, {\n status: 'canceled',\n });\n\n if (!updatedOrganization) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization.id,\n });\n }\n\n logger.info(\n `Cancelled plan for organization ${updatedOrganization.id} - ${updatedOrganization.plan?.type} - ${updatedOrganization.plan?.period}`\n );\n\n return updatedOrganization.plan ?? null;\n};\n\nexport const changeSubscriptionStatus = async (\n subscriptionId: string,\n status: Plan['status'],\n userId: string,\n organizationId: string\n): Promise<Plan | null> => {\n const organization = await getOrganizationById(organizationId);\n\n if (!organization) {\n throw new GenericError('ORGANIZATION_NOT_FOUND', {\n userId,\n subscriptionId,\n });\n }\n\n if (!organization.plan) {\n throw new GenericError('ORGANIZATION_PLAN_NOT_FOUND', {\n userId,\n subscriptionId,\n organizationId: organization.id,\n });\n }\n\n const updatedOrganization = await updatePlan(organization, {\n status,\n subscriptionId,\n });\n\n if (!updatedOrganization) {\n throw new GenericError('ORGANIZATION_UPDATE_FAILED', {\n organizationId: organization.id,\n });\n }\n\n const user = await getUserById(userId);\n\n if (!user) {\n throw new GenericError('USER_NOT_FOUND', {\n userId,\n subscriptionId,\n });\n }\n\n logger.info(\n `Updated plan status for organization ${organization.id} - Status: ${status}`\n );\n\n const emailData = {\n to: user.email,\n username: user.name,\n email: user.email,\n planName: organization.plan.type,\n date: new Date().toLocaleDateString(),\n link: `${process.env.CLIENT_URL}/dashboard`,\n };\n\n switch (status) {\n case 'active':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentSuccess',\n organizationName: organization.name,\n subscriptionStartDate: emailData.date,\n manageSubscriptionLink: emailData.link,\n });\n break;\n case 'canceled':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentCancellation',\n organizationName: organization.name,\n cancellationDate: emailData.date,\n reactivateLink: emailData.link,\n });\n break;\n case 'incomplete':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentError',\n organizationName: organization.name,\n errorDate: emailData.date,\n retryPaymentLink: emailData.link,\n });\n break;\n default:\n logger.warn(`Unhandled subscription status: ${status}`);\n }\n\n return updatedOrganization.plan ?? null;\n};\n\nexport const getCouponId = async (\n promoCode: string\n): Promise<string | null> => {\n const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\n\n try {\n // Retrieve the coupon details by name\n const coupons = await stripe.coupons.list();\n const matchingCoupon = coupons.data.find(\n (coupon) => coupon.name === promoCode\n );\n\n return matchingCoupon ? matchingCoupon.id : null;\n } catch (error) {\n console.error('Error retrieving coupon:', error);\n return null;\n }\n};\n\nexport type PricingResult = Record<\n string,\n {\n originalTotal: number;\n discountApplied: number;\n discountType: 'amount' | 'percentage' | null;\n finalTotal: number;\n currency: string;\n }\n>;\n\nexport const getPricing = async (\n priceIds: string[],\n promoCode?: string\n): Promise<PricingResult> => {\n const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);\n\n try {\n // 1. Fetch all price objects\n const pricePromises = priceIds.map((priceId) =>\n stripe.prices.retrieve(priceId)\n );\n const prices = await Promise.all(pricePromises);\n\n // Calculate the total amount before discount (to help with proportional distribution if needed)\n const totalAmount = prices.reduce(\n (sum, price) => sum + (price.unit_amount ?? 0),\n 0\n );\n\n // 2. Retrieve the discount (if promo code is provided)\n let discountAmount = 0;\n let discountType: 'amount' | 'percentage' | null = null;\n\n if (promoCode) {\n const coupons = await stripe.coupons.list();\n const matchingCoupons = coupons.data.find(\n (coupon) => coupon.name === promoCode\n );\n if (matchingCoupons) {\n if (matchingCoupons.amount_off) {\n discountAmount = matchingCoupons.amount_off;\n discountType = 'amount';\n } else if (matchingCoupons.percent_off) {\n // For a percentage discount, we won't store discountAmount as a raw number\n // because each price line is discounted individually by the same percentage.\n discountAmount = matchingCoupons.percent_off;\n discountType = 'percentage';\n }\n }\n }\n\n // 3. Build the result for each priceId\n const results: PricingResult = {};\n\n for (const price of prices) {\n if (!price.id || !price.unit_amount) {\n continue; // Skip any invalid price\n }\n\n const originalTotal = price.unit_amount;\n let appliedDiscount = 0;\n let finalTotal = originalTotal;\n\n // Apply discount based on the discount type\n if (discountType === 'percentage' && discountAmount > 0) {\n // percentage-based discount\n appliedDiscount = (originalTotal * discountAmount) / 100;\n finalTotal = originalTotal - appliedDiscount;\n } else if (\n discountType === 'amount' &&\n totalAmount > 0 &&\n discountAmount > 0\n ) {\n // fixed amount discount - distribute proportionally\n const proportion = originalTotal / totalAmount;\n appliedDiscount = discountAmount * proportion;\n finalTotal = originalTotal - appliedDiscount;\n }\n\n // Prevent final total from going negative due to rounding\n finalTotal = Math.max(finalTotal, 0);\n\n results[price.id] = {\n originalTotal: originalTotal,\n discountApplied: appliedDiscount,\n discountType,\n finalTotal: finalTotal,\n currency: price.currency,\n };\n }\n\n return results;\n } catch (error) {\n console.error('Error calculating pricing per priceId:', error);\n throw new Error('Failed to calculate pricing breakdown.');\n }\n};\n"],"mappings":"AAEA,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAC7B,SAAS,+BAA+B;AACxC,OAAO,YAAY;AACnB,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB,kBAAkB;AAChD,SAAS,mBAAmB;AAErB,MAAM,0BAA0B,OACrC,gBACA,SACA,YACA,QACA,cACA,WACyB;AACzB,QAAM,SAAS,IAAI,OAAO,QAAQ,IAAI,iBAAkB;AACxD,QAAM,OAAO,MAAM,YAAY,MAAM;AAErC,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,aAAa,kBAAkB;AAAA,MACvC;AAAA,IACF,CAAC;AAAA,EACH;AAEA,MAAI,OAAO,KAAK,UAAU,MAAM,YAAY;AAC1C,IAAC,KAAK,aAAmC;AACzC,UAAM,KAAK,KAAK;AAAA,EAClB;AAEA,QAAM,WAAW,wBAAwB,OAAO;AAEhD,QAAM,gBAAgB,MAAM,OAAO,cAAc,KAAK;AAAA,IACpD,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,OAAO;AAAA,EACT,CAAC;AAED,MAAI,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kBAAkB,QAAQ,KAAK;AAAA,QACnC,CAAC,WAAW,OAAO,SAAS;AAAA,MAC9B;AACA,UAAI,iBAAiB;AACnB,YAAI,gBAAgB,YAAY;AAC9B,2BAAiB,gBAAgB;AACjC,yBAAe;AAAA,QACjB,WAAW,gBAAgB,aAAa;AAGtC,2BAAiB,gBAAgB;AACjC,yBAAe;AAAA,QACjB;AAAA,MACF;AAAA,IACF;AAGA,UAAM,UAAyB,CAAC;AAEhC,eAAW,SAAS,QAAQ;AAC1B,UAAI,CAAC,MAAM,MAAM,CAAC,MAAM,aAAa;AACnC;AAAA,MACF;AAEA,YAAM,gBAAgB,MAAM;AAC5B,UAAI,kBAAkB;AACtB,UAAI,aAAa;AAGjB,UAAI,iBAAiB,gBAAgB,iBAAiB,GAAG;AAEvD,0BAAmB,gBAAgB,iBAAkB;AACrD,qBAAa,gBAAgB;AAAA,MAC/B,WACE,iBAAiB,YACjB,cAAc,KACd,iBAAiB,GACjB;AAEA,cAAM,aAAa,gBAAgB;AACnC,0BAAkB,iBAAiB;AACnC,qBAAa,gBAAgB;AAAA,MAC/B;AAGA,mBAAa,KAAK,IAAI,YAAY,CAAC;AAEnC,cAAQ,MAAM,EAAE,IAAI;AAAA,QAClB;AAAA,QACA,iBAAiB;AAAA,QACjB;AAAA,QACA;AAAA,QACA,UAAU,MAAM;AAAA,MAClB;AAAA,IACF;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,0CAA0C,KAAK;AAC7D,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/tag.service.ts"],"sourcesContent":["import { TagModel } from '@models/tag.model';\nimport { GenericError } from '@utils/errors';\nimport type { TagFilters } from '@utils/filtersAndPagination/getTagFiltersAndPagination';\nimport { type TagFields, validateTag } from '@utils/validation/validateTag';\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../../src/services/tag.service.ts"],"sourcesContent":["import type { Organization } from '@/export';\nimport type { Tag, TagData, TagDocument } from '@/types/tag.types';\nimport { TagModel } from '@models/tag.model';\nimport { GenericError } from '@utils/errors';\nimport type { TagFilters } from '@utils/filtersAndPagination/getTagFiltersAndPagination';\nimport { type TagFields, validateTag } from '@utils/validation/validateTag';\nimport type { Types } from 'mongoose';\n\n/**\n * Finds tags based on filters and pagination options.\n * @param filters - MongoDB filter query.\n * @param skip - Number of documents to skip.\n * @param limit - Number of documents to limit.\n * @returns List of tags matching the filters.\n */\nexport const findTags = async (\n filters: TagFilters,\n skip = 0,\n limit = 100\n): Promise<TagDocument[]> =>\n await TagModel.find(filters).skip(skip).limit(limit);\n\n/**\n * Finds a tag by its ID.\n * @param tagId - The ID of the tag to find.\n * @returns The tag matching the ID.\n */\nexport const getTagById = async (\n tagId: string | Types.ObjectId\n): Promise<TagDocument> => {\n const tag = await TagModel.findById(tagId);\n\n if (!tag) {\n throw new GenericError('TAG_NOT_FOUND', { tagId });\n }\n\n return tag;\n};\n\nexport const getTagsByKeys = async (\n keys: string[],\n organizationId: string | Organization['id']\n): Promise<TagDocument[]> => {\n const tags = await TagModel.find({ key: { $in: keys }, organizationId });\n\n return tags;\n};\n\n/**\n * Counts the total number of tags that match the filters.\n * @param filters - MongoDB filter query.\n * @returns Total number of tags.\n */\nexport const countTags = async (filters: TagFilters): Promise<number> => {\n const result = await TagModel.countDocuments(filters);\n\n if (typeof result === 'undefined') {\n throw new GenericError('TAG_COUNT_FAILED', { filters });\n }\n\n return result;\n};\n\n/**\n * Creates a new tag in the database.\n * @param tag - The tag data to create.\n * @returns The created tag.\n */\nexport const createTag = async (tag: TagData): Promise<TagDocument> => {\n const errors = await validateTag(tag, ['key']);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('TAG_INVALID_FIELDS', { errors });\n }\n\n return await TagModel.create(tag);\n};\n\n/**\n * Updates an existing tag in the database by its ID.\n * @param tagId - The ID of the tag to update.\n * @param tag - The updated tag data.\n * @returns The updated tag.\n */\nexport const updateTagById = async (\n tagId: string | Types.ObjectId,\n tag: Partial<Tag>\n): Promise<TagDocument> => {\n const updatedKeys = Object.keys(tag) as TagFields;\n\n const errors = validateTag(tag, updatedKeys);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('TAG_INVALID_FIELDS', {\n tagId,\n errors,\n });\n }\n\n const result = await TagModel.updateOne({ _id: tagId }, tag);\n\n if (result.matchedCount === 0) {\n throw new GenericError('TAG_UPDATE_FAILED', { tagId });\n }\n\n return await getTagById(tagId);\n};\n\n/**\n * Deletes a tag from the database by its ID.\n * @param tagId - The ID of the tag to delete.\n * @returns The result of the deletion operation.\n */\nexport const deleteTagById = async (\n tagId: string | Types.ObjectId\n): Promise<TagDocument> => {\n const tag = await TagModel.findByIdAndDelete(tagId);\n\n if (!tag) {\n throw new GenericError('TAG_NOT_FOUND', { tagId });\n }\n\n return tag;\n};\n"],"mappings":"AAEA,SAAS,gBAAgB;AACzB,SAAS,oBAAoB;AAE7B,SAAyB,mBAAmB;AAUrC,MAAM,WAAW,OACtB,SACA,OAAO,GACP,QAAQ,QAER,MAAM,SAAS,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,KAAK;AAO9C,MAAM,aAAa,OACxB,UACyB;AACzB,QAAM,MAAM,MAAM,SAAS,SAAS,KAAK;AAEzC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,aAAa,iBAAiB,EAAE,MAAM,CAAC;AAAA,EACnD;AAEA,SAAO;AACT;AAEO,MAAM,gBAAgB,OAC3B,MACA,mBAC2B;AAC3B,QAAM,OAAO,MAAM,SAAS,KAAK,EAAE,KAAK,EAAE,KAAK,KAAK,GAAG,eAAe,CAAC;AAEvE,SAAO;AACT;AAOO,MAAM,YAAY,OAAO,YAAyC;AACvE,QAAM,SAAS,MAAM,SAAS,eAAe,OAAO;AAEpD,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,aAAa,oBAAoB,EAAE,QAAQ,CAAC;AAAA,EACxD;AAEA,SAAO;AACT;AAOO,MAAM,YAAY,OAAO,QAAuC;AACrE,QAAM,SAAS,MAAM,YAAY,KAAK,CAAC,KAAK,CAAC;AAE7C,MAAI,OAAO,KAAK,MAAM,EAAE,SAAS,GAAG;AAClC,UAAM,IAAI,aAAa,sBAAsB,EAAE,OAAO,CAAC;AAAA,EACzD;AAEA,SAAO,MAAM,SAAS,OAAO,GAAG;AAClC;AAQO,MAAM,gBAAgB,OAC3B,OACA,QACyB;AACzB,QAAM,cAAc,OAAO,KAAK,GAAG;AAEnC,QAAM,SAAS,YAAY,KAAK,WAAW;AAE3C,MAAI,OAAO,KAAK,MAAM,EAAE,SAAS,GAAG;AAClC,UAAM,IAAI,aAAa,sBAAsB;AAAA,MAC3C;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,SAAS,MAAM,SAAS,UAAU,EAAE,KAAK,MAAM,GAAG,GAAG;AAE3D,MAAI,OAAO,iBAAiB,GAAG;AAC7B,UAAM,IAAI,aAAa,qBAAqB,EAAE,MAAM,CAAC;AAAA,EACvD;AAEA,SAAO,MAAM,WAAW,KAAK;AAC/B;AAOO,MAAM,gBAAgB,OAC3B,UACyB;AACzB,QAAM,MAAM,MAAM,SAAS,kBAAkB,KAAK;AAElD,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,aAAa,iBAAiB,EAAE,MAAM,CAAC;AAAA,EACnD;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -3,7 +3,6 @@ import { GenericError } from "./../utils/errors/index.mjs";
|
|
|
3
3
|
import {
|
|
4
4
|
validateUser
|
|
5
5
|
} from "./../utils/validation/validateUser.mjs";
|
|
6
|
-
import { hashUserPassword } from "./sessionAuth.service.mjs";
|
|
7
6
|
const createUser = async (user) => {
|
|
8
7
|
const fieldsToCheck = ["email"];
|
|
9
8
|
const errors = validateUser(user, fieldsToCheck);
|
|
@@ -13,13 +12,7 @@ const createUser = async (user) => {
|
|
|
13
12
|
errors
|
|
14
13
|
});
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
if (user.password) {
|
|
18
|
-
const userWithHashedPassword = await hashUserPassword(user);
|
|
19
|
-
newUser = await UserModel.create(userWithHashedPassword);
|
|
20
|
-
} else {
|
|
21
|
-
newUser = await UserModel.create(user);
|
|
22
|
-
}
|
|
15
|
+
const newUser = await UserModel.create(user);
|
|
23
16
|
if (!newUser) {
|
|
24
17
|
throw new GenericError("USER_CREATION_FAILED", { userEmail: user.email });
|
|
25
18
|
}
|
|
@@ -36,36 +29,7 @@ const checkUserExists = async (email) => {
|
|
|
36
29
|
return user !== null;
|
|
37
30
|
};
|
|
38
31
|
const getUserById = async (userId) => await UserModel.findById(userId);
|
|
39
|
-
const getUsersByIds = async (userIds) => {
|
|
40
|
-
return await UserModel.find({ _id: { $in: userIds } });
|
|
41
|
-
};
|
|
42
|
-
const getUserBySession = async (sessionToken) => {
|
|
43
|
-
const user = await UserModel.findOne({
|
|
44
|
-
"session.sessionToken": sessionToken
|
|
45
|
-
});
|
|
46
|
-
if (!user) {
|
|
47
|
-
throw new GenericError("USER_NOT_FOUND", { sessionToken });
|
|
48
|
-
}
|
|
49
|
-
if (user.session?.expires && user.session.expires < /* @__PURE__ */ new Date()) {
|
|
50
|
-
throw new GenericError("USER_SESSION_EXPIRED", {
|
|
51
|
-
sessionToken,
|
|
52
|
-
userId: user.id
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return user;
|
|
56
|
-
};
|
|
57
|
-
const getUserByAccount = async (provider, providerAccountId) => {
|
|
58
|
-
const user = await UserModel.findOne({
|
|
59
|
-
provider: [{ provider, providerAccountId }]
|
|
60
|
-
});
|
|
61
|
-
if (!user) {
|
|
62
|
-
throw new GenericError("USER_NOT_FOUND", {
|
|
63
|
-
provider,
|
|
64
|
-
providerAccountId
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
return user;
|
|
68
|
-
};
|
|
32
|
+
const getUsersByIds = async (userIds) => await UserModel.find({ _id: { $in: userIds } });
|
|
69
33
|
const findUsers = async (filters, skip, limit) => {
|
|
70
34
|
return await UserModel.find(filters).skip(skip).limit(limit);
|
|
71
35
|
};
|
|
@@ -109,10 +73,8 @@ export {
|
|
|
109
73
|
createUser,
|
|
110
74
|
deleteUser,
|
|
111
75
|
findUsers,
|
|
112
|
-
getUserByAccount,
|
|
113
76
|
getUserByEmail,
|
|
114
77
|
getUserById,
|
|
115
|
-
getUserBySession,
|
|
116
78
|
getUsersByEmails,
|
|
117
79
|
getUsersByIds,
|
|
118
80
|
updateUserById
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/user.service.ts"],"sourcesContent":["import { UserModel } from '@models/user.model';\nimport { GenericError } from '@utils/errors';\nimport type { UserFilters } from '@utils/filtersAndPagination/getUserFiltersAndPagination';\nimport {\n type FieldsToCheck,\n type UserFields,\n validateUser,\n} from '@utils/validation/validateUser';\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../../src/services/user.service.ts"],"sourcesContent":["import type { User, UserDocument } from '@/types/user.types';\nimport { UserModel } from '@models/user.model';\nimport { GenericError } from '@utils/errors';\nimport type { UserFilters } from '@utils/filtersAndPagination/getUserFiltersAndPagination';\nimport {\n type FieldsToCheck,\n type UserFields,\n validateUser,\n} from '@utils/validation/validateUser';\nimport type { Types } from 'mongoose';\n\n/**\n * Creates a new user with password in the database and hashes the password.\n * @param user - User object with password not hashed.\n * @returns Created user object.\n */\nexport const createUser = async (\n user: Partial<User>\n): Promise<UserDocument> => {\n const fieldsToCheck: FieldsToCheck[] = ['email'];\n\n const errors = validateUser(user, fieldsToCheck);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('USER_INVALID_FIELDS', {\n userEmail: user.email,\n errors,\n });\n }\n\n const newUser: UserDocument = await UserModel.create(user);\n\n if (!newUser) {\n throw new GenericError('USER_CREATION_FAILED', { userEmail: user.email });\n }\n\n return newUser;\n};\n\n/**\n * Retrieves a user by email.\n * @param email - User's email.\n * @returns User object or null if no user was found.\n */\nexport const getUserByEmail = async (\n email: string\n): Promise<UserDocument | null> => {\n return await UserModel.findOne({ email });\n};\n\n/**\n * Retrieves users list by email.\n * @param emails - Users email.\n * @returns User object or null if no user was found.\n */\nexport const getUsersByEmails = async (\n emails: string[]\n): Promise<UserDocument[] | null> => {\n return await UserModel.find({ email: { $in: emails } });\n};\n\n/**\n * Checks if a user exists by email.\n * @param email - User's email.\n * @returns True if the user exists, false otherwise.\n */\nexport const checkUserExists = async (email: string): Promise<boolean> => {\n const user = await UserModel.exists({ email });\n return user !== null;\n};\n\n/**\n * Retrieves a user by ID.\n * @param userId - User's ID.\n * @returns User object or null if no user was found.\n */\nexport const getUserById = async (\n userId: string | Types.ObjectId\n): Promise<UserDocument | null> => await UserModel.findById(userId);\n\n/**\n * Retrieves a user by ID.\n * @param userId - User's ID.\n * @returns User object or null if no user was found.\n */\nexport const getUsersByIds = async (\n userIds: (string | Types.ObjectId)[]\n): Promise<UserDocument[] | null> =>\n await UserModel.find({ _id: { $in: userIds } });\n\n/**\n * Finds users based on filters and pagination options.\n * @param filters - MongoDB filter query.\n * @param skip - Number of documents to skip.\n * @param limit - Number of documents to limit.\n * @returns List of users matching the filters.\n */\nexport const findUsers = async (\n filters: UserFilters,\n skip: number,\n limit: number\n): Promise<UserDocument[]> => {\n return await UserModel.find(filters).skip(skip).limit(limit);\n};\n\n/**\n * Counts the total number of users that match the filters.\n * @param filters - MongoDB filter query.\n * @returns Total number of users.\n */\nexport const countUsers = async (filters: UserFilters): Promise<number> => {\n const count = await UserModel.countDocuments(filters);\n\n if (typeof count === 'undefined') {\n throw new GenericError('USER_COUNT_FAILED');\n }\n\n return count;\n};\n\n/**\n * Updates a user's information.\n * @param user - The user object.\n * @param updates - The updates to apply to the user.\n * @returns The updated user.\n */\nexport const updateUserById = async (\n userId: string | Types.ObjectId,\n updates: Partial<User>\n): Promise<UserDocument> => {\n const keyToValidate = Object.keys(updates) as UserFields;\n const errors = validateUser(updates, keyToValidate);\n\n if (Object.keys(errors).length > 0) {\n throw new GenericError('USER_INVALID_FIELDS', {\n userId,\n errors,\n });\n }\n\n const result = await UserModel.updateOne({ _id: userId }, { $set: updates });\n\n if (result.matchedCount === 0) {\n throw new GenericError('USER_UPDATE_FAILED', { userId });\n }\n\n const updatedUser = await UserModel.findById(userId);\n\n if (!updatedUser) {\n throw new GenericError('USER_UPDATED_USER_NOT_FOUND', { userId });\n }\n\n return updatedUser;\n};\n\n/**\n * Deletes a user from the database.\n * @param userId - The user object.\n * @returns\n */\nexport const deleteUser = async (\n userId: string | Types.ObjectId\n): Promise<UserDocument> => {\n await getUserById(userId);\n\n const user = await UserModel.findByIdAndDelete(userId);\n\n if (!user) {\n throw new GenericError('USER_NOT_FOUND', { userId });\n }\n\n return user;\n};\n"],"mappings":"AACA,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB;AAE7B;AAAA,EAGE;AAAA,OACK;AAQA,MAAM,aAAa,OACxB,SAC0B;AAC1B,QAAM,gBAAiC,CAAC,OAAO;AAE/C,QAAM,SAAS,aAAa,MAAM,aAAa;AAE/C,MAAI,OAAO,KAAK,MAAM,EAAE,SAAS,GAAG;AAClC,UAAM,IAAI,aAAa,uBAAuB;AAAA,MAC5C,WAAW,KAAK;AAAA,MAChB;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,UAAwB,MAAM,UAAU,OAAO,IAAI;AAEzD,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,aAAa,wBAAwB,EAAE,WAAW,KAAK,MAAM,CAAC;AAAA,EAC1E;AAEA,SAAO;AACT;AAOO,MAAM,iBAAiB,OAC5B,UACiC;AACjC,SAAO,MAAM,UAAU,QAAQ,EAAE,MAAM,CAAC;AAC1C;AAOO,MAAM,mBAAmB,OAC9B,WACmC;AACnC,SAAO,MAAM,UAAU,KAAK,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,CAAC;AACxD;AAOO,MAAM,kBAAkB,OAAO,UAAoC;AACxE,QAAM,OAAO,M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B,EAAE,OAAO,CAAC;AAAA,EACrD;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/user.types.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/user.types.ts"],"sourcesContent":["import { RenameId } from '@utils/mongoDB/types';\nimport type { User as BetterAuthUser, OmitId } from 'better-auth';\nimport type { Document, Model, ObjectIdToString, Types } from 'mongoose';\n\nexport interface UserData {\n email: string;\n name: string;\n phone?: string;\n dateOfBirth?: Date;\n}\n\nexport enum EmailsList {\n NEWS_LETTER = 'newsLetter',\n}\n\nexport type User = OmitId<UserData & BetterAuthUser> & {\n id: Types.ObjectId;\n emailsList?: {\n [key in EmailsList]: boolean;\n };\n customerId?: string; // Stripe customer ID\n role?: string;\n lang?: string;\n createdAt: Date;\n updatedAt: Date;\n};\n\nexport type UserAPI = ObjectIdToString<Omit<User, 'provider' | 'session'>>;\n\nexport type UserSchema = RenameId<User>;\nexport type UserModelType = Model<User>;\nexport type UserDocument = Document<unknown, {}, User> & User;\n"],"mappings":"AAWO,IAAK,aAAL,kBAAKA,gBAAL;AACL,EAAAA,YAAA,iBAAc;AADJ,SAAAA;AAAA,GAAA;","names":["EmailsList"]}
|