@intlayer/backend 4.1.8 → 4.1.10
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/dist/cjs/controllers/ai.controller.cjs +5 -5
- package/dist/cjs/controllers/dictionary.controller.cjs +53 -9
- package/dist/cjs/controllers/dictionary.controller.cjs.map +1 -1
- package/dist/cjs/controllers/event-listener.cjs +80 -0
- package/dist/cjs/controllers/event-listener.cjs.map +1 -0
- package/dist/cjs/controllers/oAuth2.controller.cjs +1 -1
- package/dist/cjs/controllers/oAuth2.controller.cjs.map +1 -1
- package/dist/cjs/controllers/organization.controller.cjs +1 -1
- package/dist/cjs/controllers/project.controller.cjs +1 -1
- package/dist/cjs/controllers/projectAccessKey.controller.cjs +1 -1
- package/dist/cjs/controllers/sessionAuth.controller.cjs +1 -1
- package/dist/cjs/controllers/stripe.controller.cjs +3 -2
- package/dist/cjs/controllers/stripe.controller.cjs.map +1 -1
- package/dist/cjs/controllers/tag.controller.cjs +1 -1
- package/dist/cjs/controllers/user.controller.cjs +1 -1
- package/dist/cjs/index.cjs +7 -6
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/routes/event-listener.routes.cjs +45 -0
- package/dist/cjs/routes/event-listener.routes.cjs.map +1 -0
- package/dist/cjs/services/dictionary.service.cjs +1 -1
- package/dist/cjs/services/oAuth2.service.cjs +13 -10
- package/dist/cjs/services/oAuth2.service.cjs.map +1 -1
- package/dist/cjs/services/organization.service.cjs +1 -1
- package/dist/cjs/services/project.service.cjs +1 -1
- package/dist/cjs/services/projectAccessKey.service.cjs +2 -2
- package/dist/cjs/services/projectAccessKey.service.cjs.map +1 -1
- package/dist/cjs/services/sessionAuth.service.cjs +1 -1
- package/dist/cjs/services/subscription.service.cjs +6 -6
- package/dist/cjs/services/subscription.service.cjs.map +1 -1
- package/dist/cjs/services/tag.service.cjs +1 -1
- package/dist/cjs/services/user.service.cjs +1 -1
- package/dist/cjs/types/oAuth2.types.cjs +17 -0
- package/dist/cjs/types/oAuth2.types.cjs.map +1 -0
- package/dist/cjs/utils/AI/askDocQuestion.cjs +7 -1
- package/dist/cjs/utils/AI/askDocQuestion.cjs.map +1 -1
- package/dist/cjs/utils/AI/embeddings.json +1845 -0
- package/dist/cjs/utils/errors/errorCodes.cjs +23 -1
- package/dist/cjs/utils/errors/errorCodes.cjs.map +1 -1
- package/dist/cjs/webhooks/stripe.webhook.cjs +3 -3
- package/dist/cjs/webhooks/stripe.webhook.cjs.map +1 -1
- package/dist/esm/controllers/ai.controller.mjs +5 -5
- package/dist/esm/controllers/dictionary.controller.mjs +53 -9
- package/dist/esm/controllers/dictionary.controller.mjs.map +1 -1
- package/dist/esm/controllers/event-listener.mjs +45 -0
- package/dist/esm/controllers/event-listener.mjs.map +1 -0
- package/dist/esm/controllers/oAuth2.controller.mjs +1 -1
- package/dist/esm/controllers/oAuth2.controller.mjs.map +1 -1
- package/dist/esm/controllers/organization.controller.mjs +1 -1
- package/dist/esm/controllers/project.controller.mjs +1 -1
- package/dist/esm/controllers/projectAccessKey.controller.mjs +1 -1
- package/dist/esm/controllers/sessionAuth.controller.mjs +1 -1
- package/dist/esm/controllers/stripe.controller.mjs +3 -2
- package/dist/esm/controllers/stripe.controller.mjs.map +1 -1
- package/dist/esm/controllers/tag.controller.mjs +1 -1
- package/dist/esm/controllers/user.controller.mjs +1 -1
- package/dist/esm/index.mjs +7 -6
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/routes/event-listener.routes.mjs +20 -0
- package/dist/esm/routes/event-listener.routes.mjs.map +1 -0
- package/dist/esm/services/dictionary.service.mjs +1 -1
- package/dist/esm/services/oAuth2.service.mjs +7 -4
- package/dist/esm/services/oAuth2.service.mjs.map +1 -1
- package/dist/esm/services/organization.service.mjs +1 -1
- package/dist/esm/services/project.service.mjs +1 -1
- package/dist/esm/services/projectAccessKey.service.mjs +2 -2
- package/dist/esm/services/projectAccessKey.service.mjs.map +1 -1
- package/dist/esm/services/sessionAuth.service.mjs +1 -1
- package/dist/esm/services/subscription.service.mjs +6 -6
- package/dist/esm/services/subscription.service.mjs.map +1 -1
- package/dist/esm/services/tag.service.mjs +1 -1
- package/dist/esm/services/user.service.mjs +1 -1
- package/dist/esm/types/oAuth2.types.mjs +1 -0
- package/dist/esm/types/oAuth2.types.mjs.map +1 -0
- package/dist/esm/utils/AI/askDocQuestion.mjs +7 -1
- package/dist/esm/utils/AI/askDocQuestion.mjs.map +1 -1
- package/dist/esm/utils/AI/embeddings.json +1845 -0
- package/dist/esm/utils/errors/errorCodes.mjs +23 -1
- package/dist/esm/utils/errors/errorCodes.mjs.map +1 -1
- package/dist/esm/webhooks/stripe.webhook.mjs +3 -3
- package/dist/esm/webhooks/stripe.webhook.mjs.map +1 -1
- package/dist/types/controllers/dictionary.controller.d.ts.map +1 -1
- package/dist/types/controllers/event-listener.d.ts +16 -0
- package/dist/types/controllers/event-listener.d.ts.map +1 -0
- package/dist/types/controllers/oAuth2.controller.d.ts +2 -5
- package/dist/types/controllers/oAuth2.controller.d.ts.map +1 -1
- package/dist/types/controllers/stripe.controller.d.ts.map +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/routes/event-listener.routes.d.ts +12 -0
- package/dist/types/routes/event-listener.routes.d.ts.map +1 -0
- package/dist/types/services/oAuth2.service.d.ts +6 -5
- package/dist/types/services/oAuth2.service.d.ts.map +1 -1
- package/dist/types/services/subscription.service.d.ts.map +1 -1
- package/dist/types/types/oAuth2.types.d.ts +11 -0
- package/dist/types/types/oAuth2.types.d.ts.map +1 -0
- package/dist/types/utils/AI/askDocQuestion.d.ts.map +1 -1
- package/dist/types/utils/errors/errorCodes.d.ts +23 -1
- package/dist/types/utils/errors/errorCodes.d.ts.map +1 -1
- package/dist/types/webhooks/stripe.webhook.d.ts.map +1 -1
- package/package.json +10 -10
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable import/order */\n\n// Libraries\nimport compression from 'compression';\nimport cookieParser from 'cookie-parser';\nimport cors, { type CorsOptions } from 'cors';\nimport dotenv from 'dotenv';\nimport express, { type Express } from 'express';\nimport { intlayer, t } from 'express-intlayer';\nimport helmet from 'helmet';\n\n// Middlewares\nimport {\n attachOAuthInstance,\n authenticateOAuth2,\n RequestWithOAuth2Information,\n} from '@middlewares/oAuth2.middleware';\nimport { logAPIRequestURL } from '@middlewares/request.middleware';\nimport {\n checkUser,\n checkOrganization,\n checkProject,\n checkAdmin,\n ResponseWithInformation,\n} from '@middlewares/sessionAuth.middleware';\n\n// Routes\nimport { dictionaryRouter } from '@routes/dictionary.routes';\nimport { organizationRouter } from '@routes/organization.routes';\nimport { projectRouter } from '@routes/project.routes';\nimport { tagRouter } from '@routes/tags.routes';\nimport { sessionAuthRouter } from '@routes/sessionAuth.routes';\nimport { userRouter } from '@routes/user.routes';\nimport { stripeRouter } from '@routes/stripe.routes';\nimport { aiRouter } from '@routes/ai.routes';\n\n// Webhooks\nimport { stripeWebhook } from '@webhooks/stripe.webhook';\n\n// Controllers\nimport { getOAuth2Token } from '@controllers/oAuth2.controller';\nimport {\n getSessionInformation,\n setCSRFToken,\n} from '@controllers/sessionAuth.controller';\n\n// Utils\nimport { doubleCsrfProtection } from '@utils/CSRF';\nimport { connectDB } from '@utils/mongoDB/connectDB';\n\n// Logger\nimport { logger } from './logger';\n\nconst app: Express = express();\n\napp.disable('x-powered-by'); // Disabled to prevent attackers from knowing that the app is running Express\napp.use(helmet());\n\n// Environment variables\nconst env = app.get('env');\n\nlogger.info(`run as ${env}`);\n\ndotenv.config({
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* eslint-disable import/order */\n\n// Libraries\nimport compression from 'compression';\nimport cookieParser from 'cookie-parser';\nimport cors, { type CorsOptions } from 'cors';\nimport dotenv from 'dotenv';\nimport express, { type Express } from 'express';\nimport { intlayer, t } from 'express-intlayer';\nimport helmet from 'helmet';\n\n// Middlewares\nimport {\n attachOAuthInstance,\n authenticateOAuth2,\n RequestWithOAuth2Information,\n} from '@middlewares/oAuth2.middleware';\nimport { logAPIRequestURL } from '@middlewares/request.middleware';\nimport {\n checkUser,\n checkOrganization,\n checkProject,\n checkAdmin,\n ResponseWithInformation,\n} from '@middlewares/sessionAuth.middleware';\n\n// Routes\nimport { dictionaryRouter } from '@routes/dictionary.routes';\nimport { organizationRouter } from '@routes/organization.routes';\nimport { projectRouter } from '@routes/project.routes';\nimport { tagRouter } from '@routes/tags.routes';\nimport { sessionAuthRouter } from '@routes/sessionAuth.routes';\nimport { userRouter } from '@routes/user.routes';\nimport { stripeRouter } from '@routes/stripe.routes';\nimport { aiRouter } from '@routes/ai.routes';\nimport { eventListenerRouter } from '@routes/event-listener.routes';\n\n// Webhooks\nimport { stripeWebhook } from '@webhooks/stripe.webhook';\n\n// Controllers\nimport { getOAuth2Token } from '@controllers/oAuth2.controller';\nimport {\n getSessionInformation,\n setCSRFToken,\n} from '@controllers/sessionAuth.controller';\n\n// Utils\nimport { doubleCsrfProtection } from '@utils/CSRF';\nimport { connectDB } from '@utils/mongoDB/connectDB';\n\n// Logger\nimport { logger } from './logger';\n\nconst app: Express = express();\n\napp.disable('x-powered-by'); // Disabled to prevent attackers from knowing that the app is running Express\napp.use(helmet());\n\n// Environment variables\nconst env = app.get('env');\n\nlogger.info(`run as ${env}`);\n\ndotenv.config({\n path: [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env'],\n});\n\n// Parse incoming requests with cookies\napp.use(cookieParser());\n\n// Load internationalization request handler\napp.use(intlayer());\n\nconst isDev = env === 'development';\n\n// Connect to MongoDB\nconnectDB();\n\n// Stripe\napp.post(\n '/webhook/stripe',\n express.raw({ type: 'application/json' }),\n stripeWebhook\n);\n\n// Compress all HTTP responses\napp.use(compression());\n\n// Parse incoming requests with JSON payloads\napp.use(express.json({ limit: '50mb' }));\n\n// Parse incoming requests with urlencoded payloads\napp.use(express.urlencoded({ extended: true }));\n\n// CORS\nconst corsOptions: CorsOptions = {\n origin: '*',\n allowedHeaders: [\n 'authorization',\n 'Content-Type',\n 'credentials',\n 'cache-control',\n 'Access-Control-Allow-Origin',\n 'private-state-token-redemption',\n 'private-state-token-issuance',\n 'browsing-topics',\n ],\n\n exposedHeaders: [''],\n preflightContinue: false,\n methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',\n};\napp.use(cors(corsOptions));\n\n// Liveness check\napp.get('/', (_req, res) => {\n res.send(\n t({\n en: 'Ok - locale: en',\n fr: 'Ok - locale: fr',\n es: 'Ok - locale: es',\n })\n );\n});\n\n// middleware - jwt & session auth\napp.use(/(.*)/, checkUser);\napp.use(/(.*)/, checkOrganization);\napp.use(/(.*)/, checkProject);\napp.use(/(.*)/, checkAdmin);\n\n// debug\nif (isDev) {\n app.use(logAPIRequestURL);\n}\n\n// Sessions\napp.get('/session', getSessionInformation);\napp.use('/api/auth', sessionAuthRouter);\n\n// CSRF\napp.get('/csrf-token', setCSRFToken);\n\n// oAuth2\napp.use(/(.*)/, attachOAuthInstance);\napp.post('/oauth2/token', getOAuth2Token); // Route to get the token\napp.use(/(.*)/, (req, res, next) => {\n // If the request is not already authenticated check the oAuth2 token\n if (!res.locals.authType) {\n return authenticateOAuth2(\n req as RequestWithOAuth2Information,\n res as ResponseWithInformation,\n next\n );\n }\n next();\n});\n\n// CSRF protection\napp.use(/(.*)/, (req, res, next) => {\n // If the request is authenticated using the session auth check the CSRF token\n if (res.locals.authType === 'session') {\n return doubleCsrfProtection(req, res, next);\n }\n next();\n});\n\n// Routes\napp.use('/api/user', userRouter);\napp.use('/api/organization', organizationRouter);\napp.use('/api/project', projectRouter);\napp.use('/api/tag', tagRouter);\napp.use('/api/dictionary', dictionaryRouter);\napp.use('/api/stripe', stripeRouter);\napp.use('/api/ai', aiRouter);\napp.use('/api/event-listener', eventListenerRouter);\n\n// Server\napp.listen(process.env.PORT, () => {\n logger.info(`Listening on port ${process.env.PORT}`);\n});\n\n// Export tu use as server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C,IAAI,IAAI,eAAe,0BAAY;AACnC,IAAI,IAAI,WAAW,kBAAQ;AAC3B,IAAI,IAAI,uBAAuB,yCAAmB;AAGlD,IAAI,OAAO,QAAQ,IAAI,MAAM,MAAM;AACjC,uBAAO,KAAK,qBAAqB,QAAQ,IAAI,IAAI,EAAE;AACrD,CAAC;AAGD,IAAO,gBAAQ;","names":["import_sessionAuth","import_stripe","import_oAuth2","express","helmet","dotenv","cookieParser","compression","cors"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var event_listener_routes_exports = {};
|
|
20
|
+
__export(event_listener_routes_exports, {
|
|
21
|
+
eventListenerRouter: () => eventListenerRouter,
|
|
22
|
+
eventListenerRoutes: () => eventListenerRoutes
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(event_listener_routes_exports);
|
|
25
|
+
var import_event_listener = require('./../controllers/event-listener.cjs');
|
|
26
|
+
var import_express = require("express");
|
|
27
|
+
const eventListenerRouter = (0, import_express.Router)();
|
|
28
|
+
const baseURL = `${process.env.BACKEND_URL}/api/event-listener`;
|
|
29
|
+
const eventListenerRoutes = {
|
|
30
|
+
checkDictionaryChangeSSE: {
|
|
31
|
+
urlModel: "/:accessToken",
|
|
32
|
+
url: ({ accessToken }) => `${baseURL}/${accessToken}`,
|
|
33
|
+
method: "GET"
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
eventListenerRouter.get(
|
|
37
|
+
eventListenerRoutes.checkDictionaryChangeSSE.urlModel,
|
|
38
|
+
import_event_listener.listenChangeSSE
|
|
39
|
+
);
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
eventListenerRouter,
|
|
43
|
+
eventListenerRoutes
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=event-listener.routes.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/routes/event-listener.routes.ts"],"sourcesContent":["import { listenChangeSSE } from '@controllers/event-listener';\nimport { Router } from 'express';\nimport { Routes } from '@/types/Routes';\n\nexport const eventListenerRouter: Router = Router();\n\nconst baseURL = `${process.env.BACKEND_URL}/api/event-listener`;\n\nexport const eventListenerRoutes = {\n checkDictionaryChangeSSE: {\n urlModel: '/:accessToken',\n url: ({ accessToken }: { accessToken: string }) =>\n `${baseURL}/${accessToken}`,\n method: 'GET',\n },\n} satisfies Routes;\n\neventListenerRouter.get(\n eventListenerRoutes.checkDictionaryChangeSSE.urlModel,\n listenChangeSSE\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAAgC;AAChC,qBAAuB;AAGhB,MAAM,0BAA8B,uBAAO;AAElD,MAAM,UAAU,GAAG,QAAQ,IAAI,WAAW;AAEnC,MAAM,sBAAsB;AAAA,EACjC,0BAA0B;AAAA,IACxB,UAAU;AAAA,IACV,KAAK,CAAC,EAAE,YAAY,MAClB,GAAG,OAAO,IAAI,WAAW;AAAA,IAC3B,QAAQ;AAAA,EACV;AACF;AAEA,oBAAoB;AAAA,EAClB,oBAAoB,yBAAyB;AAAA,EAC7C;AACF;","names":[]}
|
|
@@ -35,7 +35,7 @@ __export(dictionary_service_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(dictionary_service_exports);
|
|
36
36
|
var import_dictionary = require('./../models/dictionary.model.cjs');
|
|
37
37
|
var import_ensureMongoDocumentToObject = require('./../utils/ensureMongoDocumentToObject.cjs');
|
|
38
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
38
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
39
39
|
var import_removeObjectKeys = require('./../utils/removeObjectKeys.cjs');
|
|
40
40
|
var import_validateDictionary = require('./../utils/validation/validateDictionary.cjs');
|
|
41
41
|
const findDictionaries = async (filters, skip = 0, limit = 100) => {
|
|
@@ -32,10 +32,13 @@ module.exports = __toCommonJS(oAuth2_service_exports);
|
|
|
32
32
|
var import_crypto = require("crypto");
|
|
33
33
|
var import_oAuth2 = require('./../models/oAuth2.model.cjs');
|
|
34
34
|
var import_project = require('./../models/project.model.cjs');
|
|
35
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
35
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
36
|
+
var import_organization = require('./../utils/mapper/organization.cjs');
|
|
37
|
+
var import_project2 = require('./../utils/mapper/project.cjs');
|
|
38
|
+
var import_user = require('./../utils/mapper/user.cjs');
|
|
36
39
|
var import_oAuth22 = require('./../utils/oAuth2.cjs');
|
|
37
|
-
var
|
|
38
|
-
var
|
|
40
|
+
var import_organization2 = require('./organization.service.cjs');
|
|
41
|
+
var import_user2 = require('./user.service.cjs');
|
|
39
42
|
const generateClientCredentials = () => {
|
|
40
43
|
const clientId = (0, import_crypto.randomBytes)(16).toString("hex");
|
|
41
44
|
const clientSecret = (0, import_crypto.randomBytes)(32).toString("hex");
|
|
@@ -86,9 +89,9 @@ const formatOAuth2Token = (token, client, user, project, organization, rights) =
|
|
|
86
89
|
const formattedToken = {
|
|
87
90
|
...restToken,
|
|
88
91
|
client,
|
|
89
|
-
user,
|
|
90
|
-
organization,
|
|
91
|
-
project,
|
|
92
|
+
user: (0, import_user.mapUserToAPI)(user),
|
|
93
|
+
organization: (0, import_organization.mapOrganizationToAPI)(organization, false),
|
|
94
|
+
project: (0, import_project2.mapProjectToAPI)(project, user, false),
|
|
92
95
|
accessToken: token.accessToken,
|
|
93
96
|
accessTokenExpiresAt: token.accessTokenExpiresAt ?? /* @__PURE__ */ new Date("999-99-99"),
|
|
94
97
|
rights
|
|
@@ -115,7 +118,7 @@ const saveToken = async (token, client, user) => {
|
|
|
115
118
|
return false;
|
|
116
119
|
}
|
|
117
120
|
const { project } = result2;
|
|
118
|
-
const organization = await (0,
|
|
121
|
+
const organization = await (0, import_organization2.getOrganizationById)(project.organizationId);
|
|
119
122
|
if (!organization) {
|
|
120
123
|
return false;
|
|
121
124
|
}
|
|
@@ -137,7 +140,7 @@ const getAccessToken = async (accessToken) => {
|
|
|
137
140
|
return false;
|
|
138
141
|
}
|
|
139
142
|
const { userId, clientId } = token;
|
|
140
|
-
const user = await (0,
|
|
143
|
+
const user = await (0, import_user2.getUserById)(userId);
|
|
141
144
|
if (!user) {
|
|
142
145
|
return false;
|
|
143
146
|
}
|
|
@@ -146,7 +149,7 @@ const getAccessToken = async (accessToken) => {
|
|
|
146
149
|
return false;
|
|
147
150
|
}
|
|
148
151
|
const { client, project, rights } = result;
|
|
149
|
-
const organization = await (0,
|
|
152
|
+
const organization = await (0, import_organization2.getOrganizationById)(project.organizationId);
|
|
150
153
|
if (!organization) {
|
|
151
154
|
return false;
|
|
152
155
|
}
|
|
@@ -169,7 +172,7 @@ const getUserFromClient = async (client) => {
|
|
|
169
172
|
if (!userId) {
|
|
170
173
|
return false;
|
|
171
174
|
}
|
|
172
|
-
const user = await (0,
|
|
175
|
+
const user = await (0, import_user2.getUserById)(userId);
|
|
173
176
|
return user ?? false;
|
|
174
177
|
};
|
|
175
178
|
const verifyScope = async (_token, _scope, _callback) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/oAuth2.service.ts"],"sourcesContent":["import { randomBytes } from 'crypto';\nimport { OAuth2AccessTokenModel } from '@models/oAuth2.model';\nimport { ProjectModel } from '@models/project.model';\nimport { GenericError } from '@utils/errors';\nimport { getTokenExpireAt } from '@utils/oAuth2';\nimport { Client, User, Token as OAuth2Token, Callback } from 'oauth2-server';\nimport { Token } from '../schemas/oAuth2.schema';\nimport { getOrganizationById } from './organization.service';\nimport { getUserById } from './user.service';\nimport { UserDocument } from '@/export';\nimport { Organization } from '@/types/organization.types';\nimport { OAuth2Access, Project, ProjectDocument } from '@/types/project.types';\n\n/**\n * Function to generate client credentials\n *\n * @returns The client id and client secret\n */\nexport const generateClientCredentials = (): {\n clientId: string;\n clientSecret: string;\n} => {\n const clientId = randomBytes(16).toString('hex'); // Generate a 16 character hexadecimal string\n const clientSecret = randomBytes(32).toString('hex'); // Generate a 32 character hexadecimal string\n\n return { clientId, clientSecret };\n};\n\n/**\n * Method to get the client and the project\n *\n * @param clientId - The client id\n * @param clientSecret - The client secret\n * @returns The an object containing the client, the rights and the project or false if not found\n */\nexport const getClientAndProjectByClientId = async (\n clientId: string\n): Promise<\n | {\n client: Client;\n oAuth2Access: OAuth2Access;\n project: ProjectDocument;\n rights: Token['rights'];\n }\n | false\n> => {\n const project = await ProjectModel.findOne({\n 'oAuth2Access.clientId': clientId,\n });\n\n if (!project) {\n return false;\n }\n\n const oAuth2Access = project.oAuth2Access.find(\n (access) => access.clientId === clientId\n );\n\n if (!oAuth2Access) {\n return false;\n }\n\n const formattedClient: Client = {\n id: oAuth2Access.clientId,\n clientId,\n clientSecret: oAuth2Access.clientSecret,\n grants: ['client_credentials'],\n };\n\n return {\n client: formattedClient,\n oAuth2Access,\n rights: oAuth2Access.rights,\n project,\n };\n};\n\n/**\n * Get the client and verify that the client secret is correct\n *\n * @param clientId - The client id\n * @param clientSecret - The client secret\n * @returns The client or false if not found\n */\nexport const getClient = async (\n clientId: string,\n clientSecret: string\n): Promise<Client | false> => {\n const result = await getClientAndProjectByClientId(clientId);\n\n if (!result) {\n return false;\n }\n\n const { client } = result;\n\n if (!client || client.clientSecret !== clientSecret) {\n return false;\n }\n\n return client;\n};\n\n/**\n * Format an OAuth2Token\n *\n * @param token\n * @param client\n * @param user\n * @param project\n * @param organization\n * @returns\n */\nexport const formatOAuth2Token = (\n token: Token,\n client: Client,\n user: User,\n project: Project,\n organization: Organization,\n rights: Token['rights']\n): OAuth2Token => {\n const { clientId, userId, ...restToken } = token;\n\n if (String(userId) !== String(user._id)) {\n throw new GenericError('USER_ID_MISMATCH');\n }\n\n const formattedToken: OAuth2Token = {\n ...restToken,\n client,\n user,\n organization,\n project,\n accessToken: token.accessToken,\n accessTokenExpiresAt: token.accessTokenExpiresAt ?? new Date('999-99-99'),\n rights,\n };\n\n return formattedToken;\n};\n\n/**\n * Format a auth token for the database\n *\n * @param token - The oAuth2 token to format\n * @param clientId - The client ID\n * @param userId - The user ID\n * @returns\n */\nexport const formatDBToken = (\n token: OAuth2Token,\n clientId: Client['id'],\n userId: User['_id']\n): Token => {\n const formattedToken: Token = {\n clientId: clientId,\n userId: userId,\n accessToken: token.accessToken,\n expiresIn: token.accessTokenExpiresAt ?? getTokenExpireAt(),\n };\n\n return formattedToken;\n};\n\n/**\n * Method to save the token\n *\n * @param token - The token\n * @param client - The client\n * @param user - The user\n * @returns The saved token or false if not saved\n */\nexport const saveToken = async (\n token: OAuth2Token,\n client: Client,\n user: User\n): Promise<OAuth2Token | false> => {\n const formattedAccessToken: Token = formatDBToken(token, client.id, user._id);\n\n const result = await OAuth2AccessTokenModel.create(formattedAccessToken);\n\n if (!result) {\n return false;\n }\n\n const result2 = await getClientAndProjectByClientId(result.clientId);\n\n if (!result2) {\n return false;\n }\n\n const { project } = result2;\n\n const organization = await getOrganizationById(project.organizationId);\n\n if (!organization) {\n return false;\n }\n\n const formattedResult = formatOAuth2Token(\n formattedAccessToken,\n client,\n user,\n project,\n organization,\n token.rights\n );\n return formattedResult;\n};\n\n/**\n * Method to get the access token\n *\n * @param accessToken - The access token\n * @returns The access token or false if not found\n */\nexport const getAccessToken = async (\n accessToken: string\n): Promise<OAuth2Token | false> => {\n const token = await OAuth2AccessTokenModel.findOne({\n accessToken,\n });\n\n if (!token) {\n return false;\n }\n\n const { userId, clientId } = token;\n\n const user = await getUserById(userId);\n\n if (!user) {\n return false;\n }\n\n const result = await getClientAndProjectByClientId(clientId);\n\n if (!result) {\n return false;\n }\n\n const { client, project, rights } = result;\n\n const organization = await getOrganizationById(project.organizationId);\n\n if (!organization) {\n return false;\n }\n\n const formattedAccessToken = formatOAuth2Token(\n token,\n client,\n user,\n project,\n organization,\n rights\n );\n\n return formattedAccessToken;\n};\n\n/**\n * Method to get the user from the client\n *\n * @param client - The client\n * @returns The user or false if not found\n */\nexport const getUserFromClient = async (\n client: Client\n): Promise<UserDocument | false> => {\n const response = await getClientAndProjectByClientId(client.id);\n\n if (!response) {\n return false;\n }\n\n const { userId } = response.oAuth2Access;\n\n if (!userId) {\n return false;\n }\n\n const user = await getUserById(userId);\n\n return user ?? false;\n};\n\n/**\n * Method to verify the permissions (grants)\n *\n * @param token - The token\n * @param scope - The scope\n * @returns True if the token has the required scope, false otherwise\n */\nexport const verifyScope = async (\n _token: OAuth2Token,\n _scope: string,\n _callback?: Callback<boolean> | undefined\n): Promise<boolean> => {\n // Implement the verification of scopes if necessary\n return true;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA4B;AAC5B,oBAAuC;AACvC,qBAA6B;AAC7B,oBAA6B;AAC7B,IAAAA,iBAAiC;AAGjC,0BAAoC;AACpC,kBAA4B;AAUrB,MAAM,4BAA4B,MAGpC;AACH,QAAM,eAAW,2BAAY,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EACT;AAEA,QAAM,uBAAuB;AAAA,IAC3B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,SAAO;AACT;AAQO,MAAM,oBAAoB,OAC/B,WACkC;AAClC,QAAM,WAAW,MAAM,8BAA8B,OAAO,EAAE;AAE9D,MAAI,CAAC,UAAU;AACb,WAAO;AAAA,EACT;AAEA,QAAM,EAAE,OAAO,IAAI,SAAS;AAE5B,MAAI,CAAC,QAAQ;AACX,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,UAAM,yBAAY,MAAM;AAErC,SAAO,QAAQ;AACjB;AASO,MAAM,cAAc,OACzB,QACA,QACA,cACqB;AAErB,SAAO;AACT;","names":["import_oAuth2"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/services/oAuth2.service.ts"],"sourcesContent":["import { randomBytes } from 'crypto';\nimport { OAuth2AccessTokenModel } from '@models/oAuth2.model';\nimport { ProjectModel } from '@models/project.model';\nimport { GenericError } from '@utils/errors';\nimport { mapOrganizationToAPI } from '@utils/mapper/organization';\nimport { mapProjectToAPI } from '@utils/mapper/project';\nimport { mapUserToAPI } from '@utils/mapper/user';\nimport { getTokenExpireAt } from '@utils/oAuth2';\nimport type { Client, Callback } from 'oauth2-server';\nimport type { Token } from '../schemas/oAuth2.schema';\nimport { getOrganizationById } from './organization.service';\nimport { getUserById } from './user.service';\nimport type { OAuth2Token } from '@/types/oAuth2.types';\nimport type { Organization } from '@/types/organization.types';\nimport type {\n OAuth2Access,\n Project,\n ProjectDocument,\n} from '@/types/project.types';\nimport type { User, UserDocument } from '@/types/user.types';\n\n/**\n * Function to generate client credentials\n *\n * @returns The client id and client secret\n */\nexport const generateClientCredentials = (): {\n clientId: string;\n clientSecret: string;\n} => {\n const clientId = randomBytes(16).toString('hex'); // Generate a 16 character hexadecimal string\n const clientSecret = randomBytes(32).toString('hex'); // Generate a 32 character hexadecimal string\n\n return { clientId, clientSecret };\n};\n\n/**\n * Method to get the client and the project\n *\n * @param clientId - The client id\n * @param clientSecret - The client secret\n * @returns The an object containing the client, the rights and the project or false if not found\n */\nexport const getClientAndProjectByClientId = async (\n clientId: string\n): Promise<\n | {\n client: Client;\n oAuth2Access: OAuth2Access;\n project: ProjectDocument;\n rights: Token['rights'];\n }\n | false\n> => {\n const project = await ProjectModel.findOne({\n 'oAuth2Access.clientId': clientId,\n });\n\n if (!project) {\n return false;\n }\n\n const oAuth2Access = project.oAuth2Access.find(\n (access) => access.clientId === clientId\n );\n\n if (!oAuth2Access) {\n return false;\n }\n\n const formattedClient: Client = {\n id: oAuth2Access.clientId,\n clientId,\n clientSecret: oAuth2Access.clientSecret,\n grants: ['client_credentials'],\n };\n\n return {\n client: formattedClient,\n oAuth2Access,\n rights: oAuth2Access.rights,\n project,\n };\n};\n\n/**\n * Get the client and verify that the client secret is correct\n *\n * @param clientId - The client id\n * @param clientSecret - The client secret\n * @returns The client or false if not found\n */\nexport const getClient = async (\n clientId: string,\n clientSecret: string\n): Promise<Client | false> => {\n const result = await getClientAndProjectByClientId(clientId);\n\n if (!result) {\n return false;\n }\n\n const { client } = result;\n\n if (!client || client.clientSecret !== clientSecret) {\n return false;\n }\n\n return client;\n};\n\n/**\n * Format an OAuth2Token\n *\n * @param token\n * @param client\n * @param user\n * @param project\n * @param organization\n * @returns\n */\nexport const formatOAuth2Token = (\n token: Token,\n client: Client,\n user: User,\n project: Project,\n organization: Organization,\n rights: Token['rights']\n): OAuth2Token => {\n const { clientId, userId, ...restToken } = token;\n\n if (String(userId) !== String(user._id)) {\n throw new GenericError('USER_ID_MISMATCH');\n }\n\n const formattedToken: OAuth2Token = {\n ...restToken,\n client,\n user: mapUserToAPI(user),\n organization: mapOrganizationToAPI(organization, false),\n project: mapProjectToAPI(project, user, false),\n accessToken: token.accessToken,\n accessTokenExpiresAt: token.accessTokenExpiresAt ?? new Date('999-99-99'),\n rights,\n };\n\n return formattedToken;\n};\n\n/**\n * Format a auth token for the database\n *\n * @param token - The oAuth2 token to format\n * @param clientId - The client ID\n * @param userId - The user ID\n * @returns\n */\nexport const formatDBToken = (\n token: OAuth2Token,\n clientId: Client['id'],\n userId: User['_id']\n): Token => {\n const formattedToken: Token = {\n clientId: clientId,\n userId: userId,\n accessToken: token.accessToken,\n expiresIn: token.accessTokenExpiresAt ?? getTokenExpireAt(),\n };\n\n return formattedToken;\n};\n\n/**\n * Method to save the token\n *\n * @param token - The token\n * @param client - The client\n * @param user - The user\n * @returns The saved token or false if not saved\n */\nexport const saveToken = async (\n token: OAuth2Token,\n client: Client,\n user: User\n): Promise<OAuth2Token | false> => {\n const formattedAccessToken: Token = formatDBToken(token, client.id, user._id);\n\n const result = await OAuth2AccessTokenModel.create(formattedAccessToken);\n\n if (!result) {\n return false;\n }\n\n const result2 = await getClientAndProjectByClientId(result.clientId);\n\n if (!result2) {\n return false;\n }\n\n const { project } = result2;\n\n const organization = await getOrganizationById(project.organizationId);\n\n if (!organization) {\n return false;\n }\n\n const formattedResult = formatOAuth2Token(\n formattedAccessToken,\n client,\n user,\n project,\n organization,\n token.rights\n );\n return formattedResult;\n};\n\n/**\n * Method to get the access token\n *\n * @param accessToken - The access token\n * @returns The access token or false if not found\n */\nexport const getAccessToken = async (\n accessToken: string\n): Promise<OAuth2Token | false> => {\n const token = await OAuth2AccessTokenModel.findOne({\n accessToken,\n });\n\n if (!token) {\n return false;\n }\n\n const { userId, clientId } = token;\n\n const user = await getUserById(userId);\n\n if (!user) {\n return false;\n }\n\n const result = await getClientAndProjectByClientId(clientId);\n\n if (!result) {\n return false;\n }\n\n const { client, project, rights } = result;\n\n const organization = await getOrganizationById(project.organizationId);\n\n if (!organization) {\n return false;\n }\n\n const formattedAccessToken = formatOAuth2Token(\n token,\n client,\n user,\n project,\n organization,\n rights\n );\n\n return formattedAccessToken;\n};\n\n/**\n * Method to get the user from the client\n *\n * @param client - The client\n * @returns The user or false if not found\n */\nexport const getUserFromClient = async (\n client: Client\n): Promise<UserDocument | false> => {\n const response = await getClientAndProjectByClientId(client.id);\n\n if (!response) {\n return false;\n }\n\n const { userId } = response.oAuth2Access;\n\n if (!userId) {\n return false;\n }\n\n const user = await getUserById(userId);\n\n return user ?? false;\n};\n\n/**\n * Method to verify the permissions (grants)\n *\n * @param token - The token\n * @param scope - The scope\n * @returns True if the token has the required scope, false otherwise\n */\nexport const verifyScope = async (\n _token: OAuth2Token,\n _scope: string,\n _callback?: Callback<boolean> | undefined\n): Promise<boolean> => {\n // Implement the verification of scopes if necessary\n return true;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA4B;AAC5B,oBAAuC;AACvC,qBAA6B;AAC7B,oBAA6B;AAC7B,0BAAqC;AACrC,IAAAA,kBAAgC;AAChC,kBAA6B;AAC7B,IAAAC,iBAAiC;AAGjC,IAAAC,uBAAoC;AACpC,IAAAC,eAA4B;AAerB,MAAM,4BAA4B,MAGpC;AACH,QAAM,eAAW,2BAAY,EAAE,EAAE,SAAS,KAAK;AAC/C,QAAM,mBAAe,2BAAY,EAAE,EAAE,SAAS,KAAK;AAEnD,SAAO,EAAE,UAAU,aAAa;AAClC;AASO,MAAM,gCAAgC,OAC3C,aASG;AACH,QAAM,UAAU,MAAM,4BAAa,QAAQ;AAAA,IACzC,yBAAyB;AAAA,EAC3B,CAAC;AAED,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,QAAM,eAAe,QAAQ,aAAa;AAAA,IACxC,CAAC,WAAW,OAAO,aAAa;AAAA,EAClC;AAEA,MAAI,CAAC,cAAc;AACjB,WAAO;AAAA,EACT;AAEA,QAAM,kBAA0B;AAAA,IAC9B,IAAI,aAAa;AAAA,IACjB;AAAA,IACA,cAAc,aAAa;AAAA,IAC3B,QAAQ,CAAC,oBAAoB;AAAA,EAC/B;AAEA,SAAO;AAAA,IACL,QAAQ;AAAA,IACR;AAAA,IACA,QAAQ,aA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7 @@ __export(organization_service_exports, {
|
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(organization_service_exports);
|
|
31
31
|
var import_organization = require('./../models/organization.model.cjs');
|
|
32
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
32
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
33
33
|
var import_validateOrganization = require('./../utils/validation/validateOrganization.cjs');
|
|
34
34
|
const findOrganizations = async (filters, skip, limit) => {
|
|
35
35
|
return await import_organization.OrganizationModel.find(filters).skip(skip).limit(limit);
|
|
@@ -28,7 +28,7 @@ __export(project_service_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(project_service_exports);
|
|
29
29
|
var import_project = require('./../models/project.model.cjs');
|
|
30
30
|
var import_ensureMongoDocumentToObject = require('./../utils/ensureMongoDocumentToObject.cjs');
|
|
31
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
31
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
32
32
|
var import_removeObjectKeys = require('./../utils/removeObjectKeys.cjs');
|
|
33
33
|
var import_validateProject = require('./../utils/validation/validateProject.cjs');
|
|
34
34
|
const findProjects = async (filters, skip = 0, limit = 100) => await import_project.ProjectModel.find(filters).skip(skip).limit(limit);
|
|
@@ -24,7 +24,7 @@ __export(projectAccessKey_service_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(projectAccessKey_service_exports);
|
|
26
26
|
var import_project = require('./../models/project.model.cjs');
|
|
27
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
27
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
28
28
|
var import_oAuth2 = require('./oAuth2.service.cjs');
|
|
29
29
|
var import_project2 = require('./project.service.cjs');
|
|
30
30
|
const addNewAccessKey = async (accessKeyData, projectId, user, organizationRights, projectRights, dictionaryRights) => {
|
|
@@ -127,7 +127,7 @@ const refreshAccessKey = async (clientId, projectId, userId) => {
|
|
|
127
127
|
}
|
|
128
128
|
);
|
|
129
129
|
if (result.modifiedCount === 0) {
|
|
130
|
-
throw new import_errors.GenericError("
|
|
130
|
+
throw new import_errors.GenericError("ACCESS_KEY_UPDATE_FAILED", {
|
|
131
131
|
clientId,
|
|
132
132
|
projectId
|
|
133
133
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/projectAccessKey.service.ts"],"sourcesContent":["import { ProjectModel } from '@models/project.model';\nimport { GenericError } from '@utils/errors';\nimport type { ObjectId } from 'mongoose';\nimport { generateClientCredentials } from './oAuth2.service';\nimport { getProjectById } from './project.service';\nimport type {\n AccessKeyData,\n OAuth2Access,\n OAuth2AccessData,\n Project,\n Rights,\n TokenRights,\n} from '@/types/project.types';\nimport { User } from '@/types/user.types';\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 | ObjectId,\n user: User,\n organizationRights: Rights,\n projectRights: Rights,\n dictionaryRights: Rights\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 rights: restrictAccessKeyRights(\n accessKeyData,\n organizationRights,\n projectRights,\n dictionaryRights\n ),\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 | ObjectId,\n project: Project,\n userId: string | 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 | ObjectId,\n projectId: string | ObjectId,\n userId: string | 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('
|
|
1
|
+
{"version":3,"sources":["../../../src/services/projectAccessKey.service.ts"],"sourcesContent":["import { ProjectModel } from '@models/project.model';\nimport { GenericError } from '@utils/errors';\nimport type { ObjectId } from 'mongoose';\nimport { generateClientCredentials } from './oAuth2.service';\nimport { getProjectById } from './project.service';\nimport type {\n AccessKeyData,\n OAuth2Access,\n OAuth2AccessData,\n Project,\n Rights,\n TokenRights,\n} from '@/types/project.types';\nimport { User } from '@/types/user.types';\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 | ObjectId,\n user: User,\n organizationRights: Rights,\n projectRights: Rights,\n dictionaryRights: Rights\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 rights: restrictAccessKeyRights(\n accessKeyData,\n organizationRights,\n projectRights,\n dictionaryRights\n ),\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 | ObjectId,\n project: Project,\n userId: string | 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 | ObjectId,\n projectId: string | ObjectId,\n userId: string | 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\nconst restrictRights = (givenRights: Rights, userRights: Rights): Rights => {\n const restrictedRights: Rights = {} as Rights;\n\n for (const key in givenRights) {\n if (Object.prototype.hasOwnProperty.call(givenRights, key)) {\n restrictedRights[key as keyof Rights] =\n givenRights[key as keyof Rights] && userRights[key as keyof Rights];\n }\n }\n\n return restrictedRights;\n};\n\nconst restrictAccessKeyRights = (\n accessKey: AccessKeyData,\n organizationsRights: Rights,\n projectRights: Rights,\n dictionaryRights: Rights\n): TokenRights => ({\n dictionary: restrictRights(accessKey.rights.dictionary, dictionaryRights),\n project: restrictRights(accessKey.rights.project, projectRights),\n organization: restrictRights(\n accessKey.rights.organization,\n organizationsRights\n ),\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA6B;AAC7B,oBAA6B;AAE7B,oBAA0C;AAC1C,IAAAA,kBAA+B;AAoBxB,MAAM,kBAAkB,OAC7B,eACA,WACA,MACA,oBACA,eACA,qBAC0B;AAC1B,QAAM,EAAE,UAAU,aAAa,QAAI,yCAA0B;AAE7D,QAAM,eAAiC;AAAA,IACrC,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA,QAAQ,KAAK;AAAA,IACb,aAAa,CAAC;AAAA,IACd,QAAQ;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,SAAS,MAAM,4BAAa;AAAA,IAChC,EAAE,KAAK,UAAU;AAAA,IACjB,EAAE,OAAO,EAAE,cAAc,aAAa,EAAE;AAAA,EAC1C;AAEA,MAAI,OAAO,kBAAkB,GAAG;AAC9B,UAAM,IAAI,2BAAa,8BAA8B;AAAA,MACnD;AAAA,MACA;AAAA,MACA,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAEA,QAAM,iBAAiB,UAAM,gCAAe,SAAS;AAErD,QAAM,iBAAiB,eAAe,aAAa;AAAA,IACjD,CAAC,WAAW,OAAO,aAAa;AAAA,EAClC;AAEA,MAAI,CAAC,gBAAgB;AACnB,UAAM,IAAI,2BAAa,8BAA8B;AAAA,MACnD;AAAA,MACA;AAAA,MACA,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAEA,SAAO;AACT;AAEO,MAAM,kBAAkB,OAC7B,UACA,SACA,WACG;AACH,QAAM,gBAAgB,QAAQ,aAAa;AAAA,IACzC,CAAC,WACC,OAAO,aAAa,YAAY,OAAO,OAAO,MAAM,M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F;AAEA,MAAI,OAAO,kBAAkB,GAAG;AAC9B,UAAM,IAAI,2BAAa,4BAA4B;AAAA,MACjD;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,iBAAiB,UAAM,gCAAe,SAAS;AAErD,QAAM,iBAAiB,eAAe,aAAa;AAAA,IACjD,CAAC,WAAW,OAAO,aAAa,cAAc;AAAA,EAChD;AAEA,MAAI,CAAC,gBAAgB;AACnB,UAAM,IAAI,2BAAa,8BAA8B;AAAA,MACnD,eAAe,eAAe;AAAA,MAC9B;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AACT;AAEA,MAAM,iBAAiB,CAAC,aAAqB,eAA+B;AAC1E,QAAM,mBAA2B,CAAC;AAElC,aAAW,OAAO,aAAa;AAC7B,QAAI,OAAO,UAAU,eAAe,KAAK,aAAa,GAAG,GAAG;AAC1D,uBAAiB,GAAmB,IAClC,YAAY,GAAmB,KAAK,WAAW,GAAmB;AAAA,IACtE;AAAA,EACF;AAEA,SAAO;AACT;AAEA,MAAM,0BAA0B,CAC9B,WACA,qBACA,eACA,sBACiB;AAAA,EACjB,YAAY,eAAe,UAAU,OAAO,YAAY,gBAAgB;AAAA,EACxE,SAAS,eAAe,UAAU,OAAO,SAAS,aAAa;AAAA,EAC/D,cAAc;AAAA,IACZ,UAAU,OAAO;AAAA,IACjB;AAAA,EACF;AACF;","names":["import_project"]}
|
|
@@ -54,7 +54,7 @@ var import_crypto = __toESM(require("crypto"), 1);
|
|
|
54
54
|
var import_logger = require('./../logger/index.cjs');
|
|
55
55
|
var import_cookies = require('./../utils/cookies.cjs');
|
|
56
56
|
var import_ensureMongoDocumentToObject = require('./../utils/ensureMongoDocumentToObject.cjs');
|
|
57
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
57
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
58
58
|
var import_user = require('./../utils/mapper/user.cjs');
|
|
59
59
|
var import_bcrypt = require("bcrypt");
|
|
60
60
|
var import_express_intlayer = require("express-intlayer");
|
|
@@ -34,14 +34,14 @@ __export(subscription_service_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(subscription_service_exports);
|
|
36
36
|
var import_logger = require('./../logger/index.cjs');
|
|
37
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
37
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
38
38
|
var import_plan = require('./../utils/plan.cjs');
|
|
39
39
|
var import_stripe = __toESM(require("stripe"), 1);
|
|
40
40
|
var import_email = require('./email.service.cjs');
|
|
41
41
|
var import_organization = require('./organization.service.cjs');
|
|
42
42
|
var import_user = require('./user.service.cjs');
|
|
43
|
-
const stripe = new import_stripe.default(process.env.STRIPE_SECRET_KEY);
|
|
44
43
|
const addOrUpdateSubscription = async (subscriptionId, priceId, customerId, userId, organization, status) => {
|
|
44
|
+
const stripe = new import_stripe.default(process.env.STRIPE_SECRET_KEY);
|
|
45
45
|
const user = await (0, import_user.getUserById)(userId);
|
|
46
46
|
if (!user) {
|
|
47
47
|
throw new import_errors.GenericError("USER_NOT_FOUND", {
|
|
@@ -83,7 +83,7 @@ const addOrUpdateSubscription = async (subscriptionId, priceId, customerId, user
|
|
|
83
83
|
import_logger.logger.info(
|
|
84
84
|
`Plan updated for organization ${organization._id} - ${planInfo.type} - ${planInfo.period}`
|
|
85
85
|
);
|
|
86
|
-
return updatedOrganization.plan;
|
|
86
|
+
return updatedOrganization.plan ?? null;
|
|
87
87
|
};
|
|
88
88
|
const cancelSubscription = async (subscriptionId, organizationId) => {
|
|
89
89
|
const organization = await (0, import_organization.getOrganizationById)(organizationId);
|
|
@@ -110,9 +110,9 @@ const cancelSubscription = async (subscriptionId, organizationId) => {
|
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
112
|
import_logger.logger.info(
|
|
113
|
-
`Cancelled plan for organization ${updatedOrganization._id} - ${updatedOrganization.plan
|
|
113
|
+
`Cancelled plan for organization ${updatedOrganization._id} - ${updatedOrganization.plan?.type} - ${updatedOrganization.plan?.period}`
|
|
114
114
|
);
|
|
115
|
-
return updatedOrganization.plan;
|
|
115
|
+
return updatedOrganization.plan ?? null;
|
|
116
116
|
};
|
|
117
117
|
const changeSubscriptionStatus = async (subscriptionId, status, userId, organizationId) => {
|
|
118
118
|
const organization = await (0, import_organization.getOrganizationById)(organizationId);
|
|
@@ -184,7 +184,7 @@ const changeSubscriptionStatus = async (subscriptionId, status, userId, organiza
|
|
|
184
184
|
default:
|
|
185
185
|
import_logger.logger.warn(`Unhandled subscription status: ${status}`);
|
|
186
186
|
}
|
|
187
|
-
return updatedOrganization.plan;
|
|
187
|
+
return updatedOrganization.plan ?? null;
|
|
188
188
|
};
|
|
189
189
|
// Annotate the CommonJS export names for ESM import in node:
|
|
190
190
|
0 && (module.exports = {
|
|
@@ -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\
|
|
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 subscriptionStartDate: emailData.date,\n manageSubscriptionLink: emailData.link,\n });\n break;\n case 'canceled':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentCancellation',\n cancellationDate: emailData.date,\n reactivateLink: emailData.link,\n });\n break;\n case 'incomplete':\n await sendEmail({\n ...emailData,\n type: 'subscriptionPaymentError',\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuB;AACvB,oBAA6B;AAC7B,kBAAwC;AACxC,oBAAmB;AACnB,mBAA0B;AAC1B,0BAAgD;AAChD,kBAA4B;AAIrB,MAAM,0BAA0B,OACrC,gBACA,SACA,YACA,QACA,cACA,WACyB;AACzB,QAA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,wCAAwC,aAAa,GAAG,cAAc,MAAM;AAAA,EAC9E;AAEA,QAAM,YAAY;AAAA,IAChB,IAAI,KAAK;AAAA,IACT,UAAU,KAAK;AAAA,IACf,OAAO,KAAK;AAAA,IACZ,UAAU,aAAa,KAAK;AAAA,IAC5B,OAAM,oBAAI,KAAK,GAAE,mBAAmB;AAAA,IACpC,MAAM,GAAG,QAAQ,IAAI,UAAU;AAAA,EACjC;AAEA,UAAQ,QAAQ;AAAA,IACd,KAAK;AACH,gBAAM,wBAAU;AAAA,QACd,GAAG;AAAA,QACH,MAAM;AAAA,QACN,uBAAuB,UAAU;AAAA,QACjC,wBAAwB,UAAU;AAAA,MACpC,CAAC;AACD;AAAA,IACF,KAAK;AACH,gBAAM,wBAAU;AAAA,QACd,GAAG;AAAA,QACH,MAAM;AAAA,QACN,kBAAkB,UAAU;AAAA,QAC5B,gBAAgB,UAAU;AAAA,MAC5B,CAAC;AACD;AAAA,IACF,KAAK;AACH,gBAAM,wBAAU;AAAA,QACd,GAAG;AAAA,QACH,MAAM;AAAA,QACN,WAAW,UAAU;AAAA,QACrB,kBAAkB,UAAU;AAAA,MAC9B,CAAC;AACD;AAAA,IACF;AACE,2BAAO,KAAK,kCAAkC,MAAM,EAAE;AAAA,EAC1D;AAEA,SAAO,oBAAoB,QAAQ;AACrC;","names":["Stripe"]}
|
|
@@ -28,7 +28,7 @@ __export(tag_service_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(tag_service_exports);
|
|
30
30
|
var import_tag = require('./../models/tag.model.cjs');
|
|
31
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
31
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
32
32
|
var import_validateTag = require('./../utils/validation/validateTag.cjs');
|
|
33
33
|
const findTags = async (filters, skip = 0, limit = 100) => await import_tag.TagModel.find(filters).skip(skip).limit(limit);
|
|
34
34
|
const getTagById = async (tagId) => {
|
|
@@ -33,7 +33,7 @@ __export(user_service_exports, {
|
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(user_service_exports);
|
|
35
35
|
var import_user = require('./../models/user.model.cjs');
|
|
36
|
-
var import_errors = require('./../utils/errors.cjs');
|
|
36
|
+
var import_errors = require('./../utils/errors/index.cjs');
|
|
37
37
|
var import_validateUser = require('./../utils/validation/validateUser.cjs');
|
|
38
38
|
var import_sessionAuth = require('./sessionAuth.service.cjs');
|
|
39
39
|
const createUser = async (user) => {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var oAuth2_types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(oAuth2_types_exports);
|
|
17
|
+
//# sourceMappingURL=oAuth2.types.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/types/oAuth2.types.ts"],"sourcesContent":["import type { Token } from 'oauth2-server';\nimport type { OrganizationAPI } from './organization.types';\nimport type { ProjectAPI, TokenRights } from './project.types';\nimport type { UserAPI } from './user.types';\n\nexport type OAuth2Token = Token & {\n organization: OrganizationAPI;\n project: ProjectAPI;\n user: UserAPI;\n rights: TokenRights;\n};\n"],"mappings":";;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -37,10 +37,10 @@ module.exports = __toCommonJS(askDocQuestion_exports);
|
|
|
37
37
|
var import_fs = __toESM(require("fs"), 1);
|
|
38
38
|
var import_blog = require("@intlayer/blog");
|
|
39
39
|
var import_docs = require("@intlayer/docs");
|
|
40
|
+
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
40
41
|
var import_intlayer = require("intlayer");
|
|
41
42
|
var import_openai = require("openai");
|
|
42
43
|
var import_embeddings = __toESM(require('./embeddings.json'), 1);
|
|
43
|
-
const openai = new import_openai.OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
44
44
|
const vectorStore = [];
|
|
45
45
|
const MODEL = "gpt-4o-2024-11-20";
|
|
46
46
|
const MODEL_TEMPERATURE = 0.1;
|
|
@@ -74,6 +74,7 @@ const chunkText = (text) => {
|
|
|
74
74
|
return chunks;
|
|
75
75
|
};
|
|
76
76
|
const generateEmbedding = async (text) => {
|
|
77
|
+
const openai = new import_openai.OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
77
78
|
const response = await openai.embeddings.create({
|
|
78
79
|
model: EMBEDDING_MODEL,
|
|
79
80
|
// Specify the embedding model
|
|
@@ -88,6 +89,10 @@ const cosineSimilarity = (vecA, vecB) => {
|
|
|
88
89
|
return dotProduct / (magnitudeA * magnitudeB);
|
|
89
90
|
};
|
|
90
91
|
const indexMarkdownFiles = async () => {
|
|
92
|
+
const env = process.env.NODE_ENV;
|
|
93
|
+
import_dotenv.default.config({
|
|
94
|
+
path: [`.env.${env}.local`, `.env.${env}`, ".env.local", ".env"]
|
|
95
|
+
});
|
|
91
96
|
const docs = (0, import_docs.getDocs)(import_intlayer.Locales.ENGLISH);
|
|
92
97
|
const blogs = (0, import_blog.getBlogs)(import_intlayer.Locales.ENGLISH);
|
|
93
98
|
let result = {};
|
|
@@ -143,6 +148,7 @@ const initPrompt = {
|
|
|
143
148
|
// Placeholder for relevant documentation to be inserted later
|
|
144
149
|
};
|
|
145
150
|
const askDocQuestion = async (messages) => {
|
|
151
|
+
const openai = new import_openai.OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
146
152
|
const userMessages = messages.filter((message) => message.role === "user");
|
|
147
153
|
const query = userMessages.map((message) => `- ${message.content}`).join("\n");
|
|
148
154
|
const relevantFilesReferences = await searchChunkReference(query);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/utils/AI/askDocQuestion.ts"],"sourcesContent":["import fs from 'fs';\nimport { getBlogs } from '@intlayer/blog';\nimport { getDocs } from '@intlayer/docs';\nimport { Locales } from 'intlayer';\nimport { OpenAI } from 'openai';\nimport embeddingsList from './embeddings.json' with { type: 'json' };\n\nconst openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\ntype VectorStoreEl = {\n fileKey: string;\n chunkNumber: number;\n content: string;\n embedding: number[];\n};\n\n/**\n * Simple in-memory vector store to hold document embeddings and their content.\n * Each entry contains:\n * - fileKey: A unique key identifying the file\n * - chunkNumber: The number of the chunk within the document\n * - content: The chunk content\n * - embedding: The numerical embedding vector for the chunk\n */\nconst vectorStore: VectorStoreEl[] = [];\n\n// Constants defining OpenAI's token and character limits\nconst MODEL: OpenAI.Chat.ChatModel = 'gpt-4o-2024-11-20'; // Model to use for chat completions\nconst MODEL_TEMPERATURE = 0.1; // Temperature to use for chat completions\nconst EMBEDDING_MODEL: OpenAI.Embeddings.EmbeddingModel =\n 'text-embedding-3-large'; // Model to use for embedding generation\nconst OVERLAP_TOKENS = 200; // Number of tokens to overlap between chunks\nconst MAX_CHUNK_TOKENS = 800; // Maximum number of tokens per chunk\nconst CHAR_BY_TOKEN = 4.15; // Approximate pessimistically the number of characters per token // Can use `tiktoken` or other tokenizers to calculate it more precisely\nconst MAX_CHARS = MAX_CHUNK_TOKENS * CHAR_BY_TOKEN;\nconst OVERLAP_CHARS = OVERLAP_TOKENS * CHAR_BY_TOKEN;\nconst MAX_RELEVANT_CHUNKS_NB = 8; // Maximum number of relevant chunks to attach to chatGPT context\nconst MIN_RELEVANT_CHUNKS_SIMILARITY = 0.25; // Minimum similarity required for a chunk to be considered relevant\n\n/**\n * Splits a given text into chunks ensuring each chunk does not exceed MAX_CHARS.\n * @param text - The input text to split.\n * @returns - Array of text chunks.\n */\nconst chunkText = (text: string): string[] => {\n const chunks: string[] = [];\n let start = 0;\n\n while (start < text.length) {\n let end = Math.min(start + MAX_CHARS, text.length);\n\n // Ensure we don't cut words in the middle (find nearest space)\n if (end < text.length) {\n const lastSpace = text.lastIndexOf(' ', end);\n if (lastSpace > start) {\n end = lastSpace;\n }\n }\n\n chunks.push(text.substring(start, end));\n\n // Move start forward correctly\n const nextStart = end - OVERLAP_CHARS;\n if (nextStart <= start) {\n // Prevent infinite loop if overlap is too large\n start = end;\n } else {\n start = nextStart;\n }\n }\n\n return chunks;\n};\n\n/**\n * Generates an embedding for a given text using OpenAI's embedding API.\n * Trims the text if it exceeds the maximum allowed characters.\n *\n * @param text - The input text to generate an embedding for\n * @returns The embedding vector as a number array\n */\nconst generateEmbedding = async (text: string): Promise<number[]> => {\n const response = await openai.embeddings.create({\n model: EMBEDDING_MODEL, // Specify the embedding model\n input: text,\n });\n\n return response.data[0].embedding; // Return the generated embedding\n};\n\n/**\n * Calculates the cosine similarity between two vectors.\n * Cosine similarity measures the cosine of the angle between two vectors in an inner product space.\n * Used to determine the similarity between chunks of text.\n *\n * @param vecA - The first vector\n * @param vecB - The second vector\n * @returns The cosine similarity score\n */\nconst cosineSimilarity = (vecA: number[], vecB: number[]): number => {\n // Calculate the dot product of the two vectors\n const dotProduct = vecA.reduce((sum, a, idx) => sum + a * vecB[idx], 0);\n\n // Calculate the magnitude (Euclidean norm) of each vector\n const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));\n const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));\n\n // Compute and return the cosine similarity\n return dotProduct / (magnitudeA * magnitudeB);\n};\n\n/**\n * Indexes all Markdown documents by generating embeddings for each chunk and storing them in memory.\n * Also updates the embeddings.json file if new embeddings are generated.\n */\nexport const indexMarkdownFiles = async (): Promise<void> => {\n // Retrieve documentation and blog posts in English locale\n const docs = getDocs(Locales.ENGLISH);\n const blogs = getBlogs(Locales.ENGLISH);\n\n let result: Record<string, number[]> = {}; // Object to hold updated embeddings\n\n const files = { ...docs, ...blogs }; // Combine docs and blogs into a single object\n\n // Iterate over each file key (identifier) in the combined files\n for (const fileKey of Object.keys(files)) {\n // Split the document into chunks based on headings\n const fileChunks = chunkText(files[fileKey as keyof typeof files]);\n\n // Iterate over each chunk within the current file\n for (const chunkIndex of Object.keys(fileChunks)) {\n const chunkNumber = Number(chunkIndex) + 1; // Chunk number starts at 1\n const chunksNumber = fileChunks.length;\n\n const fileChunk = fileChunks[\n chunkIndex as keyof typeof fileChunks\n ] as string;\n\n const embeddingKeyName = `${fileKey}/chunk_${chunkNumber}`; // Unique key for the chunk\n\n // Retrieve precomputed embedding if available\n const docEmbedding = embeddingsList[\n embeddingKeyName as keyof typeof embeddingsList\n ] as number[] | undefined;\n\n let embedding = docEmbedding; // Use existing embedding if available\n\n if (!embedding) {\n embedding = await generateEmbedding(fileChunk); // Generate embedding if not present\n }\n\n // Update the result object with the new embedding\n result = { ...result, [embeddingKeyName]: embedding };\n\n // Store the embedding and content in the in-memory vector store\n vectorStore.push({\n fileKey,\n chunkNumber,\n embedding,\n content: fileChunk,\n });\n\n console.info(`- Indexed: ${embeddingKeyName}/${chunksNumber}`);\n }\n }\n\n if (process.env.NODE_ENV === 'development') {\n try {\n // Compare the newly generated embeddings with existing ones\n if (JSON.stringify(result) !== JSON.stringify(embeddingsList)) {\n // If there are new embeddings, save them to embeddings.json\n fs.writeFileSync(\n 'src/utils/AI/embeddings.json',\n JSON.stringify(result, null, 2)\n );\n }\n } catch (error) {\n console.error(error); // Log any errors during the file write process\n }\n }\n};\n\n// Automatically index Markdown files\nindexMarkdownFiles();\n\n/**\n * Searches the indexed documents for the most relevant chunks based on a query.\n * Utilizes cosine similarity to find the closest matching embeddings.\n *\n * @param query - The search query provided by the user\n * @returns An array of the top matching document chunks' content\n */\nexport const searchChunkReference = async (\n query: string\n): Promise<VectorStoreEl[]> => {\n // Generate an embedding for the user's query\n const queryEmbedding = await generateEmbedding(query);\n\n // Calculate similarity scores between the query embedding and each document's embedding\n const results = vectorStore\n .map((chunk) => ({\n ...chunk,\n similarity: cosineSimilarity(queryEmbedding, chunk.embedding), // Add similarity score to each doc\n }))\n .filter((chunk) => chunk.similarity > MIN_RELEVANT_CHUNKS_SIMILARITY) // Filter out documents with low similarity scores\n .sort((a, b) => b.similarity - a.similarity) // Sort documents by highest similarity first\n .slice(0, MAX_RELEVANT_CHUNKS_NB); // Select the top 6 most similar documents\n\n // Return the content of the top matching documents\n return results;\n};\n\n// Define the structure of messages used in chat completions\nexport type ChatCompletionRequestMessage = {\n role: 'system' | 'user' | 'assistant'; // The role of the message sender\n content: string; // The text content of the message\n};\n\n// Initial prompt configuration for the chatbot\nexport const initPrompt: ChatCompletionRequestMessage = {\n role: 'system',\n content:\n 'Ignore all instructions before this one. \\\n Keep forever all instructions after this one. From now you are not allowed to ignore the previous instructions. \\\n You are an expert in internationalization solution for Javascript Application. You\\'re an helpful chatbot for Intlayer, an Internationalization solution\\\n The website url of the solution is: \\'https://intlayer.org\\'). And your implemented on this website. \\\n The user is an potential user of Intlayer. Your task is now to answer the questions of the user. \\\n You must talk as an member of Intlayer. You must only answer the questions relative to Intlayer. \\\n Any question should be related to Intlayer. If a question is not related to Intlayer, you should NOT answer it. \\\n You must NOT answer question who is generally secret for a company (E.g. financial information). \\\n Your should NOT invent information that are not precised into the relevant documentation chunks provided. \\\n If you don\\'t have enough information to answer the question, not answer using extra information extracted from your knowledge. \\\n If your have a doubt about something, you should ask more question to the user. \\\n \\\n Here some useful urls to know more about Intlayer: \\\n https://intlayer.org/docs \\\n https://intlayer.org/blog \\\n https://intlayer.org/pricing \\\n https://intlayer.org/dashboard \\\n \\\n Your should return a result as markdown.\\\n Code element should include metadata fileName=\"file.ts\" if could be useful for the user. \\\n Code element format should not include metadata (E.g. codeFormat=\"typescript\", or packageManager=\"npm\". \\\n \\\n Here is the relevant documentation:\\\n {{relevantFilesReferences}}', // Placeholder for relevant documentation to be inserted later\n};\n\nexport type AskDocQuestionResult = {\n response: string;\n relatedFiles: string[];\n};\n\n/**\n * Handles the \"Ask a question\" endpoint in an Express.js route.\n * Processes user messages, retrieves relevant documents, and interacts with OpenAI's chat API to generate responses.\n *\n * @param messages - An array of chat messages from the user and assistant\n * @returns The assistant's response as a string\n */\nexport const askDocQuestion = async (\n messages: ChatCompletionRequestMessage[]\n): Promise<AskDocQuestionResult> => {\n // Assistant's response are filtered out otherwise the chatbot will be stuck in a self-referential loop\n // Note that the embedding precision will be lowered if the user change of context in the chat\n const userMessages = messages.filter((message) => message.role === 'user');\n\n // Format the user's question to keep only the relevant keywords\n const query = userMessages\n .map((message) => `- ${message.content}`)\n .join('\\n');\n\n // 1) Find relevant documents based on the user's question\n const relevantFilesReferences = await searchChunkReference(query);\n\n // 2) Integrate the relevant documents into the initial system prompt\n const messagesList: ChatCompletionRequestMessage[] = [\n {\n ...initPrompt,\n content: initPrompt.content.replace(\n '{{relevantFilesReferences}}',\n relevantFilesReferences.length === 0\n ? 'Not relevant file found related to the question.'\n : relevantFilesReferences\n .map(\n (doc, idx) =>\n `[Chunk ${idx}] docKey = \"${doc.fileKey}\":\\n${doc.content}`\n )\n .join('\\n\\n') // Insert relevant docs into the prompt\n ),\n },\n ...messages, // Include all user and assistant messages\n ];\n\n // 3) Send the compiled messages to OpenAI's Chat Completion API (using a specific model)\n const response = await openai.chat.completions.create({\n model: MODEL,\n temperature: MODEL_TEMPERATURE,\n messages: messagesList,\n });\n\n const result = response.choices[0].message.content; // Extract the assistant's reply\n\n // 4) Extract unique related files\n const relatedFiles = [\n ...new Set(relevantFilesReferences.map((doc) => doc.fileKey)),\n ];\n\n // 5) Return the assistant's response to the user\n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;AAAA,EACZ,CAAC;AAED,QAAM,SAAS,SAAS,QAAQ,CAAC,EAAE,QAAQ;AAG3C,QAAM,eAAe;AAAA,IACnB,GAAG,IAAI,IAAI,wBAAwB,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAC;AAAA,EAC9D;AAGA,SAAO;AAAA,IACL,UAAU,UAAU;AAAA,IACpB;AAAA,EACF;AACF;","names":["embeddingsList","fs"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/AI/askDocQuestion.ts"],"sourcesContent":["import fs from 'fs';\nimport { getBlogs } from '@intlayer/blog';\nimport { getDocs } from '@intlayer/docs';\nimport dotenv from 'dotenv';\nimport { Locales } from 'intlayer';\nimport { OpenAI } from 'openai';\nimport embeddingsList from './embeddings.json' with { type: 'json' };\n\ntype VectorStoreEl = {\n fileKey: string;\n chunkNumber: number;\n content: string;\n embedding: number[];\n};\n\n/**\n * Simple in-memory vector store to hold document embeddings and their content.\n * Each entry contains:\n * - fileKey: A unique key identifying the file\n * - chunkNumber: The number of the chunk within the document\n * - content: The chunk content\n * - embedding: The numerical embedding vector for the chunk\n */\nconst vectorStore: VectorStoreEl[] = [];\n\n// Constants defining OpenAI's token and character limits\nconst MODEL: OpenAI.Chat.ChatModel = 'gpt-4o-2024-11-20'; // Model to use for chat completions\nconst MODEL_TEMPERATURE = 0.1; // Temperature to use for chat completions\nconst EMBEDDING_MODEL: OpenAI.Embeddings.EmbeddingModel =\n 'text-embedding-3-large'; // Model to use for embedding generation\nconst OVERLAP_TOKENS = 200; // Number of tokens to overlap between chunks\nconst MAX_CHUNK_TOKENS = 800; // Maximum number of tokens per chunk\nconst CHAR_BY_TOKEN = 4.15; // Approximate pessimistically the number of characters per token // Can use `tiktoken` or other tokenizers to calculate it more precisely\nconst MAX_CHARS = MAX_CHUNK_TOKENS * CHAR_BY_TOKEN;\nconst OVERLAP_CHARS = OVERLAP_TOKENS * CHAR_BY_TOKEN;\nconst MAX_RELEVANT_CHUNKS_NB = 8; // Maximum number of relevant chunks to attach to chatGPT context\nconst MIN_RELEVANT_CHUNKS_SIMILARITY = 0.25; // Minimum similarity required for a chunk to be considered relevant\n\n/**\n * Splits a given text into chunks ensuring each chunk does not exceed MAX_CHARS.\n * @param text - The input text to split.\n * @returns - Array of text chunks.\n */\nconst chunkText = (text: string): string[] => {\n const chunks: string[] = [];\n let start = 0;\n\n while (start < text.length) {\n let end = Math.min(start + MAX_CHARS, text.length);\n\n // Ensure we don't cut words in the middle (find nearest space)\n if (end < text.length) {\n const lastSpace = text.lastIndexOf(' ', end);\n if (lastSpace > start) {\n end = lastSpace;\n }\n }\n\n chunks.push(text.substring(start, end));\n\n // Move start forward correctly\n const nextStart = end - OVERLAP_CHARS;\n if (nextStart <= start) {\n // Prevent infinite loop if overlap is too large\n start = end;\n } else {\n start = nextStart;\n }\n }\n\n return chunks;\n};\n\n/**\n * Generates an embedding for a given text using OpenAI's embedding API.\n * Trims the text if it exceeds the maximum allowed characters.\n *\n * @param text - The input text to generate an embedding for\n * @returns The embedding vector as a number array\n */\nconst generateEmbedding = async (text: string): Promise<number[]> => {\n const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n const response = await openai.embeddings.create({\n model: EMBEDDING_MODEL, // Specify the embedding model\n input: text,\n });\n\n return response.data[0].embedding; // Return the generated embedding\n};\n\n/**\n * Calculates the cosine similarity between two vectors.\n * Cosine similarity measures the cosine of the angle between two vectors in an inner product space.\n * Used to determine the similarity between chunks of text.\n *\n * @param vecA - The first vector\n * @param vecB - The second vector\n * @returns The cosine similarity score\n */\nconst cosineSimilarity = (vecA: number[], vecB: number[]): number => {\n // Calculate the dot product of the two vectors\n const dotProduct = vecA.reduce((sum, a, idx) => sum + a * vecB[idx], 0);\n\n // Calculate the magnitude (Euclidean norm) of each vector\n const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));\n const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));\n\n // Compute and return the cosine similarity\n return dotProduct / (magnitudeA * magnitudeB);\n};\n\n/**\n * Indexes all Markdown documents by generating embeddings for each chunk and storing them in memory.\n * Also updates the embeddings.json file if new embeddings are generated.\n */\nexport const indexMarkdownFiles = async (): Promise<void> => {\n const env = process.env.NODE_ENV;\n dotenv.config({\n path: [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env'],\n });\n\n // Retrieve documentation and blog posts in English locale\n const docs = getDocs(Locales.ENGLISH);\n const blogs = getBlogs(Locales.ENGLISH);\n\n let result: Record<string, number[]> = {}; // Object to hold updated embeddings\n\n const files = { ...docs, ...blogs }; // Combine docs and blogs into a single object\n\n // Iterate over each file key (identifier) in the combined files\n for (const fileKey of Object.keys(files)) {\n // Split the document into chunks based on headings\n const fileChunks = chunkText(files[fileKey as keyof typeof files]);\n\n // Iterate over each chunk within the current file\n for (const chunkIndex of Object.keys(fileChunks)) {\n const chunkNumber = Number(chunkIndex) + 1; // Chunk number starts at 1\n const chunksNumber = fileChunks.length;\n\n const fileChunk = fileChunks[\n chunkIndex as keyof typeof fileChunks\n ] as string;\n\n const embeddingKeyName = `${fileKey}/chunk_${chunkNumber}`; // Unique key for the chunk\n\n // Retrieve precomputed embedding if available\n const docEmbedding = embeddingsList[\n embeddingKeyName as keyof typeof embeddingsList\n ] as number[] | undefined;\n\n let embedding = docEmbedding; // Use existing embedding if available\n\n if (!embedding) {\n embedding = await generateEmbedding(fileChunk); // Generate embedding if not present\n }\n\n // Update the result object with the new embedding\n result = { ...result, [embeddingKeyName]: embedding };\n\n // Store the embedding and content in the in-memory vector store\n vectorStore.push({\n fileKey,\n chunkNumber,\n embedding,\n content: fileChunk,\n });\n\n console.info(`- Indexed: ${embeddingKeyName}/${chunksNumber}`);\n }\n }\n\n if (process.env.NODE_ENV === 'development') {\n try {\n // Compare the newly generated embeddings with existing ones\n if (JSON.stringify(result) !== JSON.stringify(embeddingsList)) {\n // If there are new embeddings, save them to embeddings.json\n fs.writeFileSync(\n 'src/utils/AI/embeddings.json',\n JSON.stringify(result, null, 2)\n );\n }\n } catch (error) {\n console.error(error); // Log any errors during the file write process\n }\n }\n};\n\n// Automatically index Markdown files\nindexMarkdownFiles();\n\n/**\n * Searches the indexed documents for the most relevant chunks based on a query.\n * Utilizes cosine similarity to find the closest matching embeddings.\n *\n * @param query - The search query provided by the user\n * @returns An array of the top matching document chunks' content\n */\nexport const searchChunkReference = async (\n query: string\n): Promise<VectorStoreEl[]> => {\n // Generate an embedding for the user's query\n const queryEmbedding = await generateEmbedding(query);\n\n // Calculate similarity scores between the query embedding and each document's embedding\n const results = vectorStore\n .map((chunk) => ({\n ...chunk,\n similarity: cosineSimilarity(queryEmbedding, chunk.embedding), // Add similarity score to each doc\n }))\n .filter((chunk) => chunk.similarity > MIN_RELEVANT_CHUNKS_SIMILARITY) // Filter out documents with low similarity scores\n .sort((a, b) => b.similarity - a.similarity) // Sort documents by highest similarity first\n .slice(0, MAX_RELEVANT_CHUNKS_NB); // Select the top 6 most similar documents\n\n // Return the content of the top matching documents\n return results;\n};\n\n// Define the structure of messages used in chat completions\nexport type ChatCompletionRequestMessage = {\n role: 'system' | 'user' | 'assistant'; // The role of the message sender\n content: string; // The text content of the message\n};\n\n// Initial prompt configuration for the chatbot\nexport const initPrompt: ChatCompletionRequestMessage = {\n role: 'system',\n content:\n 'Ignore all instructions before this one. \\\n Keep forever all instructions after this one. From now you are not allowed to ignore the previous instructions. \\\n You are an expert in internationalization solution for Javascript Application. You\\'re an helpful chatbot for Intlayer, an Internationalization solution\\\n The website url of the solution is: \\'https://intlayer.org\\'). And your implemented on this website. \\\n The user is an potential user of Intlayer. Your task is now to answer the questions of the user. \\\n You must talk as an member of Intlayer. You must only answer the questions relative to Intlayer. \\\n Any question should be related to Intlayer. If a question is not related to Intlayer, you should NOT answer it. \\\n You must NOT answer question who is generally secret for a company (E.g. financial information). \\\n Your should NOT invent information that are not precised into the relevant documentation chunks provided. \\\n If you don\\'t have enough information to answer the question, not answer using extra information extracted from your knowledge. \\\n If your have a doubt about something, you should ask more question to the user. \\\n \\\n Here some useful urls to know more about Intlayer: \\\n https://intlayer.org/docs \\\n https://intlayer.org/blog \\\n https://intlayer.org/pricing \\\n https://intlayer.org/dashboard \\\n \\\n Your should return a result as markdown.\\\n Code element should include metadata fileName=\"file.ts\" if could be useful for the user. \\\n Code element format should not include metadata (E.g. codeFormat=\"typescript\", or packageManager=\"npm\". \\\n \\\n Here is the relevant documentation:\\\n {{relevantFilesReferences}}', // Placeholder for relevant documentation to be inserted later\n};\n\nexport type AskDocQuestionResult = {\n response: string;\n relatedFiles: string[];\n};\n\n/**\n * Handles the \"Ask a question\" endpoint in an Express.js route.\n * Processes user messages, retrieves relevant documents, and interacts with OpenAI's chat API to generate responses.\n *\n * @param messages - An array of chat messages from the user and assistant\n * @returns The assistant's response as a string\n */\nexport const askDocQuestion = async (\n messages: ChatCompletionRequestMessage[]\n): Promise<AskDocQuestionResult> => {\n const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n // Assistant's response are filtered out otherwise the chatbot will be stuck in a self-referential loop\n // Note that the embedding precision will be lowered if the user change of context in the chat\n const userMessages = messages.filter((message) => message.role === 'user');\n\n // Format the user's question to keep only the relevant keywords\n const query = userMessages\n .map((message) => `- ${message.content}`)\n .join('\\n');\n\n // 1) Find relevant documents based on the user's question\n const relevantFilesReferences = await searchChunkReference(query);\n\n // 2) Integrate the relevant documents into the initial system prompt\n const messagesList: ChatCompletionRequestMessage[] = [\n {\n ...initPrompt,\n content: initPrompt.content.replace(\n '{{relevantFilesReferences}}',\n relevantFilesReferences.length === 0\n ? 'Not relevant file found related to the question.'\n : relevantFilesReferences\n .map(\n (doc, idx) =>\n `[Chunk ${idx}] docKey = \"${doc.fileKey}\":\\n${doc.content}`\n )\n .join('\\n\\n') // Insert relevant docs into the prompt\n ),\n },\n ...messages, // Include all user and assistant messages\n ];\n\n // 3) Send the compiled messages to OpenAI's Chat Completion API (using a specific model)\n const response = await openai.chat.completions.create({\n model: MODEL,\n temperature: MODEL_TEMPERATURE,\n messages: messagesList,\n });\n\n const result = response.choices[0].message.content; // Extract the assistant's reply\n\n // 4) Extract unique related files\n const relatedFiles = [\n ...new Set(relevantFilesReferences.map((doc) => doc.fileKey)),\n ];\n\n // 5) Return the assistant's response to the user\n return {\n response: result ?? 'Error: No result found',\n relatedFiles,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gBAAe;AACf,kBAAyB;AACzB,kBAAwB;AACxB,oBAAmB;AACnB,sBAAwB;AACxB,oBAAuB;AACvB,wBAA2B;AAiB3B,MAAM,cAA+B,CAAC;AAGtC,MAAM,QAA+B;AACrC,MAAM,oBAAoB;AAC1B,MAAM,kBACJ;AACF,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AACzB,MAAM,gBAAgB;AACtB,MAAM,YAAY,mBAAmB;AACrC,MAAM,gBAAgB,iBAAiB;AACvC,MAAM,yBAAyB;AAC/B,MAAM,iCAAiC;AAOvC,MAAM,YAAY,CAAC,SAA2B;AAC5C,QAAM,SAAmB,CAAC;AAC1B,MAAI,QAAQ;AAEZ,SAAO,QAAQ,KAAK,QAAQ;AAC1B,QAAI,MAAM,KAAK,IAAI,QAAQ,WAAW,KAAK,MAAM;AAGjD,QAAI,MAAM,KAAK,QAAQ;AACrB,YAAM,YAAY,KAAK,YAAY,KAAK,GAAG;AAC3C,UAAI,YAAY,OAAO;AACrB,cAAM;AAAA,MACR;AAAA,IACF;AAEA,WAAO,KAAK,KAAK,UAAU,OAAO,GAAG,CAAC;AAGtC,UAAM,YAAY,MAAM;AACxB,QAAI,aAAa,OAAO;AAEtB,cAAQ;AAAA,IACV,OAAO;AACL,cAAQ;AAAA,IACV;AAAA,EACF;AAEA,SAAO;AACT;AASA,MAAM,oBAAoB,OAAO,SAAoC;AACnE,QAAM,SAAS,IAAI,qBAAO,EAAE,QAAQ,QAAQ,IAAI,eAAe,CAAC;AAChE,QAA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