@eeplatform/core 1.8.9 → 1.8.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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +21 -30
- package/dist/index.js +1328 -1099
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1154 -927
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -643,24 +643,24 @@ function useAuthService() {
|
|
|
643
643
|
}
|
|
644
644
|
|
|
645
645
|
// src/resources/auth/auth.controller.ts
|
|
646
|
-
import
|
|
646
|
+
import Joi3 from "joi";
|
|
647
647
|
import {
|
|
648
|
-
AppError as
|
|
649
|
-
BadRequestError as
|
|
648
|
+
AppError as AppError4,
|
|
649
|
+
BadRequestError as BadRequestError12,
|
|
650
650
|
InternalServerError as InternalServerError10,
|
|
651
651
|
logger as logger9
|
|
652
652
|
} from "@eeplatform/nodejs-utils";
|
|
653
653
|
|
|
654
654
|
// src/resources/user/user.service.ts
|
|
655
655
|
import {
|
|
656
|
-
BadRequestError as
|
|
656
|
+
BadRequestError as BadRequestError11,
|
|
657
657
|
InternalServerError as InternalServerError9,
|
|
658
658
|
NotFoundError as NotFoundError3,
|
|
659
659
|
hashPassword,
|
|
660
660
|
logger as logger8,
|
|
661
661
|
makeCacheKey as makeCacheKey7,
|
|
662
|
-
useAtlas as
|
|
663
|
-
useCache as
|
|
662
|
+
useAtlas as useAtlas7,
|
|
663
|
+
useCache as useCache7,
|
|
664
664
|
useS3
|
|
665
665
|
} from "@eeplatform/nodejs-utils";
|
|
666
666
|
|
|
@@ -1264,6 +1264,43 @@ function useMemberRepo() {
|
|
|
1264
1264
|
);
|
|
1265
1265
|
}
|
|
1266
1266
|
}
|
|
1267
|
+
async function getByRole(role) {
|
|
1268
|
+
try {
|
|
1269
|
+
role = new ObjectId8(role);
|
|
1270
|
+
} catch (error) {
|
|
1271
|
+
throw new BadRequestError7("Invalid role ID.");
|
|
1272
|
+
}
|
|
1273
|
+
try {
|
|
1274
|
+
const cacheKey = makeCacheKey5(namespace_collection, {
|
|
1275
|
+
role: role.toString()
|
|
1276
|
+
});
|
|
1277
|
+
const cached = await getCache(cacheKey);
|
|
1278
|
+
if (cached) {
|
|
1279
|
+
logger5.log({
|
|
1280
|
+
level: "info",
|
|
1281
|
+
message: `Cache hit for getById member: ${cacheKey}`
|
|
1282
|
+
});
|
|
1283
|
+
return cached;
|
|
1284
|
+
}
|
|
1285
|
+
const data = await collection.findOne({ role });
|
|
1286
|
+
setCache(cacheKey, data, 300).then(() => {
|
|
1287
|
+
logger5.log({
|
|
1288
|
+
level: "info",
|
|
1289
|
+
message: `Cache set for member by role: ${cacheKey}`
|
|
1290
|
+
});
|
|
1291
|
+
}).catch((err) => {
|
|
1292
|
+
logger5.log({
|
|
1293
|
+
level: "error",
|
|
1294
|
+
message: `Failed to set cache for member by role: ${err.message}`
|
|
1295
|
+
});
|
|
1296
|
+
});
|
|
1297
|
+
return data;
|
|
1298
|
+
} catch (error) {
|
|
1299
|
+
throw new InternalServerError6(
|
|
1300
|
+
"Internal server error, failed to retrieve member."
|
|
1301
|
+
);
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1267
1304
|
async function getByOrg(org) {
|
|
1268
1305
|
try {
|
|
1269
1306
|
org = new ObjectId8(org);
|
|
@@ -1747,6 +1784,7 @@ function useMemberRepo() {
|
|
|
1747
1784
|
createIndexes,
|
|
1748
1785
|
add,
|
|
1749
1786
|
getById,
|
|
1787
|
+
getByRole,
|
|
1750
1788
|
getByOrg,
|
|
1751
1789
|
getAll,
|
|
1752
1790
|
getOrgsByUserId,
|
|
@@ -2095,134 +2133,121 @@ import { ObjectId as ObjectId11 } from "mongodb";
|
|
|
2095
2133
|
|
|
2096
2134
|
// src/resources/role/role.repository.ts
|
|
2097
2135
|
import {
|
|
2098
|
-
BadRequestError as
|
|
2136
|
+
BadRequestError as BadRequestError10,
|
|
2099
2137
|
InternalServerError as InternalServerError8,
|
|
2100
|
-
useAtlas as useAtlas7,
|
|
2101
2138
|
paginate as paginate4,
|
|
2102
2139
|
logger as logger7,
|
|
2103
2140
|
makeCacheKey as makeCacheKey6,
|
|
2104
|
-
|
|
2141
|
+
AppError as AppError3,
|
|
2142
|
+
useRepo
|
|
2105
2143
|
} from "@eeplatform/nodejs-utils";
|
|
2106
2144
|
|
|
2107
2145
|
// src/resources/role/role.model.ts
|
|
2146
|
+
import { BadRequestError as BadRequestError9 } from "@eeplatform/nodejs-utils";
|
|
2147
|
+
import Joi2 from "joi";
|
|
2108
2148
|
import { ObjectId as ObjectId9 } from "mongodb";
|
|
2109
|
-
var
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2149
|
+
var schemaRole = Joi2.object({
|
|
2150
|
+
name: Joi2.string().required(),
|
|
2151
|
+
description: Joi2.string().max(1024).optional().allow("", null),
|
|
2152
|
+
permissions: Joi2.array().items(Joi2.string()).required(),
|
|
2153
|
+
org: Joi2.string().hex().optional().allow("", null),
|
|
2154
|
+
app: Joi2.string().required(),
|
|
2155
|
+
createdBy: Joi2.string().hex().required()
|
|
2156
|
+
});
|
|
2157
|
+
var schemaRoleUpdate = Joi2.object({
|
|
2158
|
+
name: Joi2.string().optional().allow("", null),
|
|
2159
|
+
description: Joi2.string().max(1024).optional().allow("", null),
|
|
2160
|
+
permissions: Joi2.array().items(Joi2.string()).optional().allow("", null)
|
|
2161
|
+
});
|
|
2162
|
+
function modelRole(value) {
|
|
2163
|
+
const { error } = schemaRole.validate(value);
|
|
2164
|
+
if (error) {
|
|
2165
|
+
throw new BadRequestError9(error.message);
|
|
2166
|
+
}
|
|
2167
|
+
if (value._id && typeof value._id === "string") {
|
|
2168
|
+
try {
|
|
2169
|
+
value._id = new ObjectId9(value._id);
|
|
2170
|
+
} catch (error2) {
|
|
2171
|
+
throw new BadRequestError9("Invalid _id.");
|
|
2117
2172
|
}
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2173
|
+
}
|
|
2174
|
+
if (value.org && typeof value.org === "string" && value.org.length === 24) {
|
|
2175
|
+
try {
|
|
2176
|
+
value.org = new ObjectId9(value.org);
|
|
2177
|
+
} catch (error2) {
|
|
2178
|
+
throw new BadRequestError9("Invalid org.");
|
|
2124
2179
|
}
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
this.status = value.status ?? "active";
|
|
2132
|
-
this.default = value.default ?? false;
|
|
2133
|
-
if (value.createdBy) {
|
|
2134
|
-
try {
|
|
2135
|
-
value.createdBy = new ObjectId9(value.createdBy);
|
|
2136
|
-
} catch (error) {
|
|
2137
|
-
throw new Error("Invalid createdBy.");
|
|
2138
|
-
}
|
|
2180
|
+
}
|
|
2181
|
+
if (value.createdBy && typeof value.createdBy === "string" && value.createdBy.length === 24) {
|
|
2182
|
+
try {
|
|
2183
|
+
value.createdBy = new ObjectId9(value.createdBy);
|
|
2184
|
+
} catch (error2) {
|
|
2185
|
+
throw new BadRequestError9("Invalid createdBy.");
|
|
2139
2186
|
}
|
|
2140
|
-
this.createdBy = value.createdBy ?? "";
|
|
2141
|
-
this.createdAt = value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
2142
|
-
this.updatedAt = value.updatedAt ?? "";
|
|
2143
|
-
this.deletedAt = value.deletedAt ?? "";
|
|
2144
2187
|
}
|
|
2145
|
-
|
|
2188
|
+
return {
|
|
2189
|
+
_id: value._id,
|
|
2190
|
+
name: value.name,
|
|
2191
|
+
description: value.description ?? "",
|
|
2192
|
+
permissions: value.permissions,
|
|
2193
|
+
org: value.org,
|
|
2194
|
+
app: value.app,
|
|
2195
|
+
status: value.status ?? "active",
|
|
2196
|
+
createdBy: value.createdBy,
|
|
2197
|
+
createdAt: value.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
2198
|
+
updatedAt: "",
|
|
2199
|
+
deletedAt: ""
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2146
2202
|
|
|
2147
2203
|
// src/resources/role/role.repository.ts
|
|
2148
2204
|
import { ObjectId as ObjectId10 } from "mongodb";
|
|
2149
2205
|
function useRoleRepo() {
|
|
2150
|
-
const db = useAtlas7.getDb();
|
|
2151
|
-
if (!db) {
|
|
2152
|
-
throw new InternalServerError8("Unable to connect to server.");
|
|
2153
|
-
}
|
|
2154
2206
|
const namespace_collection = "roles";
|
|
2155
|
-
const
|
|
2156
|
-
const { getCache, setCache, delNamespace } = useCache7(namespace_collection);
|
|
2157
|
-
function delCachedData() {
|
|
2158
|
-
delNamespace().then(() => {
|
|
2159
|
-
logger7.log({
|
|
2160
|
-
level: "info",
|
|
2161
|
-
message: `Cache namespace cleared for ${namespace_collection}`
|
|
2162
|
-
});
|
|
2163
|
-
}).catch((err) => {
|
|
2164
|
-
logger7.log({
|
|
2165
|
-
level: "error",
|
|
2166
|
-
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
2167
|
-
});
|
|
2168
|
-
});
|
|
2169
|
-
}
|
|
2207
|
+
const repo = useRepo(namespace_collection);
|
|
2170
2208
|
async function createIndexes() {
|
|
2171
2209
|
try {
|
|
2172
|
-
await collection.createIndexes([
|
|
2173
|
-
{
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
name: "text_index"
|
|
2178
|
-
},
|
|
2179
|
-
{
|
|
2180
|
-
key: { name: 1, type: 1, status: 1, id: 1 },
|
|
2181
|
-
unique: true
|
|
2182
|
-
}
|
|
2210
|
+
await repo.collection.createIndexes([
|
|
2211
|
+
{ key: { name: 1 } },
|
|
2212
|
+
{ key: { status: 1 } },
|
|
2213
|
+
{ key: { name: "text" } },
|
|
2214
|
+
{ key: { name: 1, app: 1, org: 1, status: 1 }, unique: true }
|
|
2183
2215
|
]);
|
|
2184
2216
|
} catch (error) {
|
|
2185
|
-
throw new InternalServerError8("Failed to create role indexes.");
|
|
2186
|
-
}
|
|
2187
|
-
}
|
|
2188
|
-
async function createUniqueIndex() {
|
|
2189
|
-
try {
|
|
2190
|
-
await collection.createIndex(
|
|
2191
|
-
{ name: 1, type: 1, id: 1 },
|
|
2192
|
-
{ unique: true }
|
|
2193
|
-
);
|
|
2194
|
-
} catch (error) {
|
|
2195
|
-
throw new InternalServerError8("Failed to create unique index on role.");
|
|
2196
2217
|
}
|
|
2197
2218
|
}
|
|
2198
2219
|
async function addRole(value, session, clearCache = true) {
|
|
2199
|
-
value = new MRole(value);
|
|
2200
2220
|
try {
|
|
2201
|
-
|
|
2221
|
+
value = modelRole(value);
|
|
2222
|
+
const res = await repo.collection.insertOne(value, { session });
|
|
2202
2223
|
if (clearCache) {
|
|
2203
|
-
delCachedData();
|
|
2224
|
+
repo.delCachedData();
|
|
2204
2225
|
}
|
|
2205
2226
|
return res.insertedId;
|
|
2206
2227
|
} catch (error) {
|
|
2207
2228
|
logger7.log({ level: "error", message: `${error}` });
|
|
2208
2229
|
const isDuplicated = error.message.includes("duplicate");
|
|
2209
2230
|
if (isDuplicated) {
|
|
2210
|
-
throw new
|
|
2231
|
+
throw new BadRequestError10("Role already exists");
|
|
2232
|
+
}
|
|
2233
|
+
if (error instanceof AppError3) {
|
|
2234
|
+
throw error;
|
|
2235
|
+
} else {
|
|
2236
|
+
throw new InternalServerError8("Failed to create role.");
|
|
2211
2237
|
}
|
|
2212
|
-
throw new InternalServerError8("Failed to create role.");
|
|
2213
2238
|
}
|
|
2214
2239
|
}
|
|
2215
2240
|
async function getRoleByUserId(value) {
|
|
2216
2241
|
try {
|
|
2217
2242
|
value = new ObjectId10(value);
|
|
2218
2243
|
} catch (error) {
|
|
2219
|
-
throw new
|
|
2244
|
+
throw new BadRequestError10("Invalid user ID.");
|
|
2220
2245
|
}
|
|
2221
2246
|
try {
|
|
2222
2247
|
const cacheKey = makeCacheKey6(namespace_collection, {
|
|
2223
2248
|
user: String(value)
|
|
2224
2249
|
});
|
|
2225
|
-
const cached = await getCache(cacheKey);
|
|
2250
|
+
const cached = await repo.getCache(cacheKey);
|
|
2226
2251
|
if (cached) {
|
|
2227
2252
|
logger7.log({
|
|
2228
2253
|
level: "info",
|
|
@@ -2230,8 +2255,8 @@ function useRoleRepo() {
|
|
|
2230
2255
|
});
|
|
2231
2256
|
return cached;
|
|
2232
2257
|
}
|
|
2233
|
-
const data = await collection.findOne({ user: value });
|
|
2234
|
-
setCache(cacheKey, data, 300).then(() => {
|
|
2258
|
+
const data = await repo.collection.findOne({ user: value });
|
|
2259
|
+
repo.setCache(cacheKey, data, 300).then(() => {
|
|
2235
2260
|
logger7.log({
|
|
2236
2261
|
level: "info",
|
|
2237
2262
|
message: `Cache set for role by user ID: ${cacheKey}`
|
|
@@ -2247,26 +2272,26 @@ function useRoleRepo() {
|
|
|
2247
2272
|
throw new InternalServerError8("Failed to retrieve role by user ID.");
|
|
2248
2273
|
}
|
|
2249
2274
|
}
|
|
2250
|
-
async function
|
|
2275
|
+
async function getById(_id) {
|
|
2251
2276
|
try {
|
|
2252
2277
|
_id = new ObjectId10(_id);
|
|
2253
2278
|
} catch (error) {
|
|
2254
|
-
throw new
|
|
2279
|
+
throw new BadRequestError10("Invalid ID.");
|
|
2255
2280
|
}
|
|
2256
2281
|
try {
|
|
2257
2282
|
const cacheKey = makeCacheKey6(namespace_collection, {
|
|
2258
2283
|
id: _id.toString()
|
|
2259
2284
|
});
|
|
2260
|
-
const cached = await getCache(cacheKey);
|
|
2285
|
+
const cached = await repo.getCache(cacheKey);
|
|
2261
2286
|
if (cached) {
|
|
2262
2287
|
logger7.log({
|
|
2263
2288
|
level: "info",
|
|
2264
|
-
message: `Cache hit for
|
|
2289
|
+
message: `Cache hit for getById role: ${cacheKey}`
|
|
2265
2290
|
});
|
|
2266
2291
|
return cached;
|
|
2267
2292
|
}
|
|
2268
|
-
const data = await collection.findOne({ _id });
|
|
2269
|
-
setCache(cacheKey, data, 300).then(() => {
|
|
2293
|
+
const data = await repo.collection.findOne({ _id });
|
|
2294
|
+
repo.setCache(cacheKey, data, 300).then(() => {
|
|
2270
2295
|
logger7.log({
|
|
2271
2296
|
level: "info",
|
|
2272
2297
|
message: `Cache set for role by id: ${cacheKey}`
|
|
@@ -2284,13 +2309,13 @@ function useRoleRepo() {
|
|
|
2284
2309
|
}
|
|
2285
2310
|
async function getRoleByName(name) {
|
|
2286
2311
|
if (!name) {
|
|
2287
|
-
throw new
|
|
2312
|
+
throw new BadRequestError10("Role name is required.");
|
|
2288
2313
|
}
|
|
2289
2314
|
try {
|
|
2290
2315
|
const cacheKey = makeCacheKey6(namespace_collection, {
|
|
2291
2316
|
name
|
|
2292
2317
|
});
|
|
2293
|
-
const cached = await getCache(cacheKey);
|
|
2318
|
+
const cached = await repo.getCache(cacheKey);
|
|
2294
2319
|
if (cached) {
|
|
2295
2320
|
logger7.log({
|
|
2296
2321
|
level: "info",
|
|
@@ -2298,8 +2323,8 @@ function useRoleRepo() {
|
|
|
2298
2323
|
});
|
|
2299
2324
|
return cached;
|
|
2300
2325
|
}
|
|
2301
|
-
const data = await collection.findOne({ name });
|
|
2302
|
-
setCache(cacheKey, data, 300).then(() => {
|
|
2326
|
+
const data = await repo.collection.findOne({ name });
|
|
2327
|
+
repo.setCache(cacheKey, data, 300).then(() => {
|
|
2303
2328
|
logger7.log({
|
|
2304
2329
|
level: "info",
|
|
2305
2330
|
message: `Cache set for role by name: ${cacheKey}`
|
|
@@ -2320,34 +2345,29 @@ function useRoleRepo() {
|
|
|
2320
2345
|
page = 1,
|
|
2321
2346
|
limit = 10,
|
|
2322
2347
|
sort = {},
|
|
2323
|
-
|
|
2324
|
-
|
|
2348
|
+
app = "",
|
|
2349
|
+
org = ""
|
|
2325
2350
|
} = {}) {
|
|
2326
2351
|
limit = limit > 0 ? limit : 10;
|
|
2327
2352
|
search = search || "";
|
|
2328
2353
|
page = page > 0 ? page - 1 : 0;
|
|
2329
|
-
if (id && typeof id === "string" && id.length === 24) {
|
|
2330
|
-
try {
|
|
2331
|
-
id = new ObjectId10(id);
|
|
2332
|
-
} catch (error) {
|
|
2333
|
-
throw new BadRequestError9("Invalid ID.");
|
|
2334
|
-
}
|
|
2335
|
-
}
|
|
2336
2354
|
const query = { status: "active" };
|
|
2337
|
-
if (id) {
|
|
2338
|
-
query.id = id;
|
|
2339
|
-
}
|
|
2340
2355
|
const cacheKeyOptions = {
|
|
2341
2356
|
status: "active",
|
|
2342
2357
|
limit,
|
|
2343
2358
|
page
|
|
2344
2359
|
};
|
|
2345
|
-
if (
|
|
2346
|
-
|
|
2360
|
+
if (org && typeof org === "string" && org.length === 24) {
|
|
2361
|
+
try {
|
|
2362
|
+
query.org = new ObjectId10(org);
|
|
2363
|
+
} catch (error) {
|
|
2364
|
+
throw new BadRequestError10("Invalid ID.");
|
|
2365
|
+
}
|
|
2366
|
+
cacheKeyOptions.org = org;
|
|
2347
2367
|
}
|
|
2348
|
-
if (
|
|
2349
|
-
cacheKeyOptions.
|
|
2350
|
-
query.
|
|
2368
|
+
if (app) {
|
|
2369
|
+
cacheKeyOptions.app = app;
|
|
2370
|
+
query.app = app;
|
|
2351
2371
|
}
|
|
2352
2372
|
if (search) {
|
|
2353
2373
|
cacheKeyOptions.search = search;
|
|
@@ -2356,7 +2376,7 @@ function useRoleRepo() {
|
|
|
2356
2376
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
2357
2377
|
cacheKeyOptions.sort = JSON.stringify(sort);
|
|
2358
2378
|
const cacheKey = makeCacheKey6(namespace_collection, cacheKeyOptions);
|
|
2359
|
-
const cached = await getCache(cacheKey);
|
|
2379
|
+
const cached = await repo.getCache(cacheKey);
|
|
2360
2380
|
if (cached) {
|
|
2361
2381
|
logger7.log({
|
|
2362
2382
|
level: "info",
|
|
@@ -2365,15 +2385,15 @@ function useRoleRepo() {
|
|
|
2365
2385
|
return cached;
|
|
2366
2386
|
}
|
|
2367
2387
|
try {
|
|
2368
|
-
const items = await collection.aggregate([
|
|
2388
|
+
const items = await repo.collection.aggregate([
|
|
2369
2389
|
{ $match: query },
|
|
2370
2390
|
{ $sort: sort },
|
|
2371
2391
|
{ $skip: page * limit },
|
|
2372
2392
|
{ $limit: limit }
|
|
2373
2393
|
]).toArray();
|
|
2374
|
-
const length = await collection.countDocuments(query);
|
|
2394
|
+
const length = await repo.collection.countDocuments(query);
|
|
2375
2395
|
const data = paginate4(items, page, limit, length);
|
|
2376
|
-
setCache(cacheKey, data, 600).then(() => {
|
|
2396
|
+
repo.setCache(cacheKey, data, 600).then(() => {
|
|
2377
2397
|
logger7.log({
|
|
2378
2398
|
level: "info",
|
|
2379
2399
|
message: `Cache set for getRoles: ${cacheKey}`
|
|
@@ -2391,74 +2411,64 @@ function useRoleRepo() {
|
|
|
2391
2411
|
}
|
|
2392
2412
|
}
|
|
2393
2413
|
async function updateRole(_id, value, session) {
|
|
2394
|
-
|
|
2395
|
-
|
|
2414
|
+
const { error } = schemaRoleUpdate.validate(value);
|
|
2415
|
+
if (error) {
|
|
2416
|
+
throw new BadRequestError10(error.message);
|
|
2396
2417
|
}
|
|
2397
2418
|
try {
|
|
2398
2419
|
_id = new ObjectId10(_id);
|
|
2399
|
-
} catch (
|
|
2400
|
-
throw new
|
|
2401
|
-
}
|
|
2402
|
-
if (!value.name) {
|
|
2403
|
-
delete value.name;
|
|
2404
|
-
}
|
|
2405
|
-
if (!value.permissions) {
|
|
2406
|
-
delete value.permissions;
|
|
2420
|
+
} catch (error2) {
|
|
2421
|
+
throw new BadRequestError10("Invalid role ID.");
|
|
2407
2422
|
}
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
throw new InternalServerError8("Failed to update role.");
|
|
2415
|
-
}
|
|
2416
|
-
} else {
|
|
2417
|
-
throw new BadRequestError9("No fields to update.");
|
|
2423
|
+
try {
|
|
2424
|
+
await repo.collection.updateOne({ _id }, { $set: value }, { session });
|
|
2425
|
+
repo.delCachedData();
|
|
2426
|
+
return "Successfully updated role.";
|
|
2427
|
+
} catch (error2) {
|
|
2428
|
+
throw new InternalServerError8("Failed to update role.");
|
|
2418
2429
|
}
|
|
2419
2430
|
}
|
|
2420
2431
|
async function updatePermissionsById(_id, permissions, session) {
|
|
2421
2432
|
if (!_id) {
|
|
2422
|
-
throw new
|
|
2433
|
+
throw new BadRequestError10("Role ID is required.");
|
|
2423
2434
|
}
|
|
2424
2435
|
try {
|
|
2425
2436
|
_id = new ObjectId10(_id);
|
|
2426
2437
|
} catch (error) {
|
|
2427
|
-
throw new
|
|
2438
|
+
throw new BadRequestError10("Invalid role ID.");
|
|
2428
2439
|
}
|
|
2429
2440
|
if (!permissions) {
|
|
2430
|
-
throw new
|
|
2441
|
+
throw new BadRequestError10("Permissions are required.");
|
|
2431
2442
|
}
|
|
2432
2443
|
if (permissions.length === 0) {
|
|
2433
|
-
throw new
|
|
2444
|
+
throw new BadRequestError10("Permissions cannot be empty.");
|
|
2434
2445
|
}
|
|
2435
2446
|
try {
|
|
2436
|
-
await collection.updateOne(
|
|
2447
|
+
await repo.collection.updateOne(
|
|
2437
2448
|
{ _id },
|
|
2438
2449
|
{ $set: { permissions } },
|
|
2439
2450
|
{ session }
|
|
2440
2451
|
);
|
|
2441
|
-
delCachedData();
|
|
2452
|
+
repo.delCachedData();
|
|
2442
2453
|
return "Successfully updated role permissions.";
|
|
2443
2454
|
} catch (error) {
|
|
2444
2455
|
throw new InternalServerError8("Failed to update role permissions.");
|
|
2445
2456
|
}
|
|
2446
2457
|
}
|
|
2447
|
-
async function
|
|
2458
|
+
async function deleteById(_id, session) {
|
|
2448
2459
|
try {
|
|
2449
2460
|
_id = new ObjectId10(_id);
|
|
2450
2461
|
} catch (error) {
|
|
2451
|
-
throw new
|
|
2462
|
+
throw new BadRequestError10("Invalid ID.");
|
|
2452
2463
|
}
|
|
2453
2464
|
try {
|
|
2454
|
-
await collection.
|
|
2465
|
+
await repo.collection.deleteOne(
|
|
2455
2466
|
{ _id },
|
|
2456
|
-
{ $set: { status: "deleted", updateAt: /* @__PURE__ */ new Date() } },
|
|
2457
2467
|
{
|
|
2458
2468
|
session
|
|
2459
2469
|
}
|
|
2460
2470
|
);
|
|
2461
|
-
delCachedData();
|
|
2471
|
+
repo.delCachedData();
|
|
2462
2472
|
return "Successfully deleted role.";
|
|
2463
2473
|
} catch (error) {
|
|
2464
2474
|
throw new InternalServerError8("Failed to delete role.");
|
|
@@ -2466,16 +2476,15 @@ function useRoleRepo() {
|
|
|
2466
2476
|
}
|
|
2467
2477
|
return {
|
|
2468
2478
|
createIndexes,
|
|
2469
|
-
createUniqueIndex,
|
|
2470
2479
|
addRole,
|
|
2471
2480
|
getRoles,
|
|
2472
2481
|
getRoleByUserId,
|
|
2473
|
-
|
|
2482
|
+
getById,
|
|
2474
2483
|
getRoleByName,
|
|
2475
2484
|
updateRole,
|
|
2476
|
-
|
|
2485
|
+
deleteById,
|
|
2477
2486
|
updatePermissionsById,
|
|
2478
|
-
delCachedData
|
|
2487
|
+
delCachedData: repo.delCachedData
|
|
2479
2488
|
};
|
|
2480
2489
|
}
|
|
2481
2490
|
|
|
@@ -2493,12 +2502,12 @@ function useUserService() {
|
|
|
2493
2502
|
const { addRole } = useRoleRepo();
|
|
2494
2503
|
const { add: addMember } = useMemberRepo();
|
|
2495
2504
|
async function createDefaultUser() {
|
|
2496
|
-
const session =
|
|
2505
|
+
const session = useAtlas7.getClient()?.startSession();
|
|
2497
2506
|
try {
|
|
2498
2507
|
session?.startTransaction();
|
|
2499
2508
|
const _user = await getUserByEmail(DEFAULT_USER_EMAIL);
|
|
2500
2509
|
if (_user) {
|
|
2501
|
-
throw new
|
|
2510
|
+
throw new BadRequestError11(
|
|
2502
2511
|
`User already exists: ${DEFAULT_USER_EMAIL}.`
|
|
2503
2512
|
);
|
|
2504
2513
|
}
|
|
@@ -2519,10 +2528,9 @@ function useUserService() {
|
|
|
2519
2528
|
{
|
|
2520
2529
|
id: "",
|
|
2521
2530
|
name: "Super Admin",
|
|
2522
|
-
|
|
2531
|
+
app: "admin",
|
|
2523
2532
|
permissions: ["*"],
|
|
2524
|
-
status: "active"
|
|
2525
|
-
default: true
|
|
2533
|
+
status: "active"
|
|
2526
2534
|
},
|
|
2527
2535
|
session
|
|
2528
2536
|
);
|
|
@@ -2559,12 +2567,12 @@ function useUserService() {
|
|
|
2559
2567
|
}
|
|
2560
2568
|
}
|
|
2561
2569
|
async function createUser(value) {
|
|
2562
|
-
const session =
|
|
2570
|
+
const session = useAtlas7.getClient()?.startSession();
|
|
2563
2571
|
session?.startTransaction();
|
|
2564
2572
|
try {
|
|
2565
2573
|
const _user = await getUserByEmail(value.email);
|
|
2566
2574
|
if (_user) {
|
|
2567
|
-
throw new
|
|
2575
|
+
throw new BadRequestError11(`User already exists: ${value.email}.`);
|
|
2568
2576
|
}
|
|
2569
2577
|
const hashedPassword = await hashPassword(value.password);
|
|
2570
2578
|
const userId = new ObjectId11();
|
|
@@ -2598,19 +2606,19 @@ function useUserService() {
|
|
|
2598
2606
|
lastName = "",
|
|
2599
2607
|
password = ""
|
|
2600
2608
|
} = {}) {
|
|
2601
|
-
const session =
|
|
2609
|
+
const session = useAtlas7.getClient()?.startSession();
|
|
2602
2610
|
session?.startTransaction();
|
|
2603
2611
|
try {
|
|
2604
2612
|
const invitation = await _getVerificationById(id);
|
|
2605
2613
|
if (!invitation || !invitation.metadata?.app || !invitation.metadata?.role) {
|
|
2606
|
-
throw new
|
|
2614
|
+
throw new BadRequestError11("Invalid invitation.");
|
|
2607
2615
|
}
|
|
2608
2616
|
if (invitation.status === "complete") {
|
|
2609
|
-
throw new
|
|
2617
|
+
throw new BadRequestError11("Invitation already used.");
|
|
2610
2618
|
}
|
|
2611
2619
|
const expired = new Date(invitation.expireAt) < /* @__PURE__ */ new Date();
|
|
2612
2620
|
if (invitation.status === "expired" || expired) {
|
|
2613
|
-
throw new
|
|
2621
|
+
throw new BadRequestError11("Invitation expired.");
|
|
2614
2622
|
}
|
|
2615
2623
|
const email = invitation.email;
|
|
2616
2624
|
const _user = await getUserByEmail(invitation.email);
|
|
@@ -2657,26 +2665,26 @@ function useUserService() {
|
|
|
2657
2665
|
lastName = "",
|
|
2658
2666
|
password = ""
|
|
2659
2667
|
} = {}) {
|
|
2660
|
-
const session =
|
|
2668
|
+
const session = useAtlas7.getClient()?.startSession();
|
|
2661
2669
|
session?.startTransaction();
|
|
2662
2670
|
try {
|
|
2663
2671
|
const signUp = await _getVerificationById(id);
|
|
2664
2672
|
if (!signUp) {
|
|
2665
|
-
throw new
|
|
2673
|
+
throw new BadRequestError11("Invalid sign up link.");
|
|
2666
2674
|
}
|
|
2667
2675
|
if (signUp.status === "complete") {
|
|
2668
|
-
throw new
|
|
2676
|
+
throw new BadRequestError11(
|
|
2669
2677
|
"You have already an account created using this link."
|
|
2670
2678
|
);
|
|
2671
2679
|
}
|
|
2672
2680
|
const expired = new Date(signUp.expireAt) < /* @__PURE__ */ new Date();
|
|
2673
2681
|
if (signUp.status === "expired" || expired) {
|
|
2674
|
-
throw new
|
|
2682
|
+
throw new BadRequestError11("Sign up link expired.");
|
|
2675
2683
|
}
|
|
2676
2684
|
const email = signUp.email;
|
|
2677
2685
|
const _user = await getUserByEmail(signUp.email);
|
|
2678
2686
|
if (_user) {
|
|
2679
|
-
throw new
|
|
2687
|
+
throw new BadRequestError11(`User already exists: ${email}.`);
|
|
2680
2688
|
}
|
|
2681
2689
|
const hashedPassword = await hashPassword(password);
|
|
2682
2690
|
const userId = new ObjectId11();
|
|
@@ -2710,7 +2718,7 @@ function useUserService() {
|
|
|
2710
2718
|
throw error;
|
|
2711
2719
|
}
|
|
2712
2720
|
if (newPassword !== passwordConfirmation) {
|
|
2713
|
-
throw new
|
|
2721
|
+
throw new BadRequestError11("Passwords do not match.");
|
|
2714
2722
|
}
|
|
2715
2723
|
let hashedPassword = "";
|
|
2716
2724
|
try {
|
|
@@ -2724,7 +2732,7 @@ function useUserService() {
|
|
|
2724
2732
|
throw new NotFoundError3("You are using an invalid reset link.");
|
|
2725
2733
|
}
|
|
2726
2734
|
if (otpDoc.status === "used") {
|
|
2727
|
-
throw new
|
|
2735
|
+
throw new BadRequestError11("This link has already been invalidated.");
|
|
2728
2736
|
}
|
|
2729
2737
|
await updateStatusById(id, "used");
|
|
2730
2738
|
return "Successfully reset password.";
|
|
@@ -2735,19 +2743,19 @@ function useUserService() {
|
|
|
2735
2743
|
const { updateName: updateMemberName } = useMemberRepo();
|
|
2736
2744
|
async function updateName(_id, firstName, lastName) {
|
|
2737
2745
|
if (!_id) {
|
|
2738
|
-
throw new
|
|
2746
|
+
throw new BadRequestError11("Invalid user ID");
|
|
2739
2747
|
}
|
|
2740
2748
|
if (!firstName) {
|
|
2741
|
-
throw new
|
|
2749
|
+
throw new BadRequestError11("Invalid firstName");
|
|
2742
2750
|
}
|
|
2743
2751
|
if (!lastName) {
|
|
2744
|
-
throw new
|
|
2752
|
+
throw new BadRequestError11("Invalid lastName");
|
|
2745
2753
|
}
|
|
2746
|
-
const session =
|
|
2754
|
+
const session = useAtlas7.getClient()?.startSession();
|
|
2747
2755
|
session?.startTransaction();
|
|
2748
2756
|
const cacheKey = makeCacheKey7("users", { user: _id });
|
|
2749
2757
|
try {
|
|
2750
|
-
|
|
2758
|
+
useCache7().delCache(cacheKey).then(() => {
|
|
2751
2759
|
logger8.info(`Cache cleared for user: ${_id}`);
|
|
2752
2760
|
}).catch((error) => {
|
|
2753
2761
|
logger8.error(`Failed to clear cache for user: ${_id}`, error);
|
|
@@ -2768,16 +2776,16 @@ function useUserService() {
|
|
|
2768
2776
|
}
|
|
2769
2777
|
async function updateBirthday(_id, month, day, year) {
|
|
2770
2778
|
if (!_id) {
|
|
2771
|
-
throw new
|
|
2779
|
+
throw new BadRequestError11("Invalid user ID");
|
|
2772
2780
|
}
|
|
2773
2781
|
if (!month) {
|
|
2774
|
-
throw new
|
|
2782
|
+
throw new BadRequestError11("Invalid birth month.");
|
|
2775
2783
|
}
|
|
2776
2784
|
if (!day) {
|
|
2777
|
-
throw new
|
|
2785
|
+
throw new BadRequestError11("Invalid birthday.");
|
|
2778
2786
|
}
|
|
2779
2787
|
if (!year) {
|
|
2780
|
-
throw new
|
|
2788
|
+
throw new BadRequestError11("Invalid birth year.");
|
|
2781
2789
|
}
|
|
2782
2790
|
try {
|
|
2783
2791
|
await _updateBirthday({ _id, month, day, year });
|
|
@@ -2802,7 +2810,7 @@ function useUserService() {
|
|
|
2802
2810
|
bucket: SPACES_BUCKET
|
|
2803
2811
|
});
|
|
2804
2812
|
async function updateUserProfile({ file, user, previousProfile } = {}) {
|
|
2805
|
-
const session =
|
|
2813
|
+
const session = useAtlas7.getClient()?.startSession();
|
|
2806
2814
|
session?.startTransaction();
|
|
2807
2815
|
const _file = {
|
|
2808
2816
|
name: file.originalname,
|
|
@@ -2852,13 +2860,13 @@ function useAuthController() {
|
|
|
2852
2860
|
async function login(req, res, next) {
|
|
2853
2861
|
const email = req.body.email;
|
|
2854
2862
|
const password = req.body.password;
|
|
2855
|
-
const validation =
|
|
2856
|
-
email:
|
|
2857
|
-
password:
|
|
2863
|
+
const validation = Joi3.object({
|
|
2864
|
+
email: Joi3.string().email().required(),
|
|
2865
|
+
password: Joi3.string().required()
|
|
2858
2866
|
});
|
|
2859
2867
|
const { error } = validation.validate({ email, password });
|
|
2860
2868
|
if (error) {
|
|
2861
|
-
next(new
|
|
2869
|
+
next(new BadRequestError12(error.message));
|
|
2862
2870
|
return;
|
|
2863
2871
|
}
|
|
2864
2872
|
try {
|
|
@@ -2878,7 +2886,7 @@ function useAuthController() {
|
|
|
2878
2886
|
level: "error",
|
|
2879
2887
|
message: `Error during login: ${error2.message}`
|
|
2880
2888
|
});
|
|
2881
|
-
if (error2 instanceof
|
|
2889
|
+
if (error2 instanceof AppError4) {
|
|
2882
2890
|
next(error2);
|
|
2883
2891
|
} else {
|
|
2884
2892
|
next(new InternalServerError10("An unexpected error occurred"));
|
|
@@ -2889,14 +2897,14 @@ function useAuthController() {
|
|
|
2889
2897
|
async function refreshToken(req, res, next) {
|
|
2890
2898
|
const refreshToken2 = req.body.token;
|
|
2891
2899
|
if (!refreshToken2) {
|
|
2892
|
-
next(new
|
|
2900
|
+
next(new BadRequestError12("Refresh token is required"));
|
|
2893
2901
|
return;
|
|
2894
2902
|
}
|
|
2895
2903
|
try {
|
|
2896
2904
|
const newRefreshToken = await useAuthService().refreshToken(refreshToken2);
|
|
2897
2905
|
res.json({ token: newRefreshToken });
|
|
2898
2906
|
} catch (error) {
|
|
2899
|
-
if (error instanceof
|
|
2907
|
+
if (error instanceof AppError4) {
|
|
2900
2908
|
next(error);
|
|
2901
2909
|
} else {
|
|
2902
2910
|
next(new InternalServerError10("An unexpected error occurred"));
|
|
@@ -2906,14 +2914,14 @@ function useAuthController() {
|
|
|
2906
2914
|
async function logout(req, res, next) {
|
|
2907
2915
|
const sid = req.headers["authorization"] ?? "";
|
|
2908
2916
|
if (!sid) {
|
|
2909
|
-
next(new
|
|
2917
|
+
next(new BadRequestError12("Session ID is required"));
|
|
2910
2918
|
return;
|
|
2911
2919
|
}
|
|
2912
2920
|
try {
|
|
2913
2921
|
await useAuthService().logout(sid);
|
|
2914
2922
|
res.json({ message: "Logged out successfully" });
|
|
2915
2923
|
} catch (error) {
|
|
2916
|
-
if (error instanceof
|
|
2924
|
+
if (error instanceof AppError4) {
|
|
2917
2925
|
next(error);
|
|
2918
2926
|
} else {
|
|
2919
2927
|
next(new InternalServerError10("An unexpected error occurred"));
|
|
@@ -2924,10 +2932,10 @@ function useAuthController() {
|
|
|
2924
2932
|
const otp = req.body.otp || "";
|
|
2925
2933
|
const newPassword = req.body.newPassword || "";
|
|
2926
2934
|
const passwordConfirmation = req.body.passwordConfirmation || "";
|
|
2927
|
-
const validation =
|
|
2928
|
-
otp:
|
|
2929
|
-
newPassword:
|
|
2930
|
-
passwordConfirmation:
|
|
2935
|
+
const validation = Joi3.object({
|
|
2936
|
+
otp: Joi3.string().hex().required(),
|
|
2937
|
+
newPassword: Joi3.string().required().min(8),
|
|
2938
|
+
passwordConfirmation: Joi3.string().required().min(8)
|
|
2931
2939
|
});
|
|
2932
2940
|
const { error } = validation.validate({
|
|
2933
2941
|
otp,
|
|
@@ -2935,7 +2943,7 @@ function useAuthController() {
|
|
|
2935
2943
|
passwordConfirmation
|
|
2936
2944
|
});
|
|
2937
2945
|
if (error) {
|
|
2938
|
-
next(new
|
|
2946
|
+
next(new BadRequestError12(error.message));
|
|
2939
2947
|
return;
|
|
2940
2948
|
}
|
|
2941
2949
|
try {
|
|
@@ -2953,13 +2961,13 @@ function useAuthController() {
|
|
|
2953
2961
|
async function signUp(req, res, next) {
|
|
2954
2962
|
const email = req.body.email;
|
|
2955
2963
|
const referralCode = req.body.referral ?? "";
|
|
2956
|
-
const validation =
|
|
2957
|
-
email:
|
|
2958
|
-
referralCode:
|
|
2964
|
+
const validation = Joi3.object({
|
|
2965
|
+
email: Joi3.string().email().required(),
|
|
2966
|
+
referralCode: Joi3.string().min(4).optional().allow("", null)
|
|
2959
2967
|
});
|
|
2960
2968
|
const { error } = validation.validate({ email, referralCode });
|
|
2961
2969
|
if (error) {
|
|
2962
|
-
next(new
|
|
2970
|
+
next(new BadRequestError12(error.message));
|
|
2963
2971
|
return;
|
|
2964
2972
|
}
|
|
2965
2973
|
try {
|
|
@@ -2986,11 +2994,11 @@ function useAuthController() {
|
|
|
2986
2994
|
|
|
2987
2995
|
// src/resources/user/user.controller.ts
|
|
2988
2996
|
import {
|
|
2989
|
-
AppError as
|
|
2990
|
-
BadRequestError as
|
|
2997
|
+
AppError as AppError5,
|
|
2998
|
+
BadRequestError as BadRequestError13,
|
|
2991
2999
|
InternalServerError as InternalServerError11
|
|
2992
3000
|
} from "@eeplatform/nodejs-utils";
|
|
2993
|
-
import
|
|
3001
|
+
import Joi4 from "joi";
|
|
2994
3002
|
function useUserController() {
|
|
2995
3003
|
const {
|
|
2996
3004
|
updateName: _updateName,
|
|
@@ -3006,14 +3014,14 @@ function useUserController() {
|
|
|
3006
3014
|
const status = req.query.status ?? "";
|
|
3007
3015
|
const search = req.query.search ?? "";
|
|
3008
3016
|
const page = Number(req.query.page) ?? 1;
|
|
3009
|
-
const validation =
|
|
3010
|
-
status:
|
|
3011
|
-
search:
|
|
3012
|
-
page:
|
|
3017
|
+
const validation = Joi4.object({
|
|
3018
|
+
status: Joi4.string().required(),
|
|
3019
|
+
search: Joi4.string().optional().allow("", null),
|
|
3020
|
+
page: Joi4.number().required()
|
|
3013
3021
|
});
|
|
3014
3022
|
const { error } = validation.validate({ status, search, page });
|
|
3015
3023
|
if (error) {
|
|
3016
|
-
next(new
|
|
3024
|
+
next(new BadRequestError13(error.message));
|
|
3017
3025
|
return;
|
|
3018
3026
|
}
|
|
3019
3027
|
try {
|
|
@@ -3026,14 +3034,14 @@ function useUserController() {
|
|
|
3026
3034
|
}
|
|
3027
3035
|
async function getUserById(req, res, next) {
|
|
3028
3036
|
const id = req.params.id || "";
|
|
3029
|
-
const validation =
|
|
3037
|
+
const validation = Joi4.string().hex().validate(id);
|
|
3030
3038
|
if (validation.error) {
|
|
3031
|
-
throw new
|
|
3039
|
+
throw new BadRequestError13("Invalid id.");
|
|
3032
3040
|
}
|
|
3033
3041
|
try {
|
|
3034
3042
|
const user = await _getUserById(id);
|
|
3035
3043
|
if (!user) {
|
|
3036
|
-
throw new
|
|
3044
|
+
throw new BadRequestError13("User not found.");
|
|
3037
3045
|
}
|
|
3038
3046
|
res.json(user);
|
|
3039
3047
|
} catch (error) {
|
|
@@ -3044,13 +3052,13 @@ function useUserController() {
|
|
|
3044
3052
|
const id = req.headers.user ?? "";
|
|
3045
3053
|
const firstName = req.body.firstName ?? "";
|
|
3046
3054
|
const lastName = req.body.lastName ?? "";
|
|
3047
|
-
const validation =
|
|
3048
|
-
firstName:
|
|
3049
|
-
lastName:
|
|
3055
|
+
const validation = Joi4.object({
|
|
3056
|
+
firstName: Joi4.string().required(),
|
|
3057
|
+
lastName: Joi4.string().required()
|
|
3050
3058
|
});
|
|
3051
3059
|
const { error } = validation.validate({ firstName, lastName });
|
|
3052
3060
|
if (error) {
|
|
3053
|
-
next(new
|
|
3061
|
+
next(new BadRequestError13(error.message));
|
|
3054
3062
|
return;
|
|
3055
3063
|
}
|
|
3056
3064
|
try {
|
|
@@ -3066,14 +3074,14 @@ function useUserController() {
|
|
|
3066
3074
|
const month = req.body.month ?? "";
|
|
3067
3075
|
const day = req.body.day ?? 0;
|
|
3068
3076
|
const year = req.body.year ?? 0;
|
|
3069
|
-
const validation =
|
|
3070
|
-
month:
|
|
3071
|
-
day:
|
|
3072
|
-
year:
|
|
3077
|
+
const validation = Joi4.object({
|
|
3078
|
+
month: Joi4.string().required(),
|
|
3079
|
+
day: Joi4.number().integer().min(1).max(31).required(),
|
|
3080
|
+
year: Joi4.number().integer().min(1900).max((/* @__PURE__ */ new Date()).getFullYear()).required()
|
|
3073
3081
|
});
|
|
3074
3082
|
const { error } = validation.validate({ month, day, year });
|
|
3075
3083
|
if (error) {
|
|
3076
|
-
next(new
|
|
3084
|
+
next(new BadRequestError13(error.message));
|
|
3077
3085
|
return;
|
|
3078
3086
|
}
|
|
3079
3087
|
try {
|
|
@@ -3087,18 +3095,18 @@ function useUserController() {
|
|
|
3087
3095
|
async function updateUserFieldById(req, res, next) {
|
|
3088
3096
|
const _id = req.params.id;
|
|
3089
3097
|
const { field, value } = req.body;
|
|
3090
|
-
const validation =
|
|
3091
|
-
_id:
|
|
3092
|
-
field:
|
|
3093
|
-
value:
|
|
3098
|
+
const validation = Joi4.object({
|
|
3099
|
+
_id: Joi4.string().hex().required(),
|
|
3100
|
+
field: Joi4.string().valid("gender", "email", "contact", "profile").required(),
|
|
3101
|
+
value: Joi4.alternatives().conditional("field", {
|
|
3094
3102
|
is: "email",
|
|
3095
|
-
then:
|
|
3096
|
-
otherwise:
|
|
3103
|
+
then: Joi4.string().email().required(),
|
|
3104
|
+
otherwise: Joi4.string().required()
|
|
3097
3105
|
})
|
|
3098
3106
|
});
|
|
3099
3107
|
const { error } = validation.validate({ _id, field, value });
|
|
3100
3108
|
if (error) {
|
|
3101
|
-
next(new
|
|
3109
|
+
next(new BadRequestError13(error.message));
|
|
3102
3110
|
return;
|
|
3103
3111
|
}
|
|
3104
3112
|
try {
|
|
@@ -3114,12 +3122,12 @@ function useUserController() {
|
|
|
3114
3122
|
return;
|
|
3115
3123
|
}
|
|
3116
3124
|
const previousProfile = req.body.previousProfile ?? "";
|
|
3117
|
-
const validation =
|
|
3118
|
-
previousProfile:
|
|
3125
|
+
const validation = Joi4.object({
|
|
3126
|
+
previousProfile: Joi4.string().hex().optional().allow("", null)
|
|
3119
3127
|
});
|
|
3120
3128
|
const { error } = validation.validate({ previousProfile });
|
|
3121
3129
|
if (error) {
|
|
3122
|
-
next(new
|
|
3130
|
+
next(new BadRequestError13(error.message));
|
|
3123
3131
|
return;
|
|
3124
3132
|
}
|
|
3125
3133
|
const user = req.headers["user"] ?? "";
|
|
@@ -3132,7 +3140,7 @@ function useUserController() {
|
|
|
3132
3140
|
res.json({ message: "Successfully updated profile picture." });
|
|
3133
3141
|
return;
|
|
3134
3142
|
} catch (error2) {
|
|
3135
|
-
if (error2 instanceof
|
|
3143
|
+
if (error2 instanceof AppError5) {
|
|
3136
3144
|
next(error2);
|
|
3137
3145
|
} else {
|
|
3138
3146
|
next(new InternalServerError11(error2));
|
|
@@ -3145,12 +3153,12 @@ function useUserController() {
|
|
|
3145
3153
|
const password = req.body.password ?? "";
|
|
3146
3154
|
const id = req.params.id ?? "";
|
|
3147
3155
|
const type = req.body.type ?? "";
|
|
3148
|
-
const validation =
|
|
3149
|
-
firstName:
|
|
3150
|
-
lastName:
|
|
3151
|
-
password:
|
|
3152
|
-
id:
|
|
3153
|
-
type:
|
|
3156
|
+
const validation = Joi4.object({
|
|
3157
|
+
firstName: Joi4.string().required(),
|
|
3158
|
+
lastName: Joi4.string().required(),
|
|
3159
|
+
password: Joi4.string().required(),
|
|
3160
|
+
id: Joi4.string().hex().required(),
|
|
3161
|
+
type: Joi4.string().required()
|
|
3154
3162
|
});
|
|
3155
3163
|
const { error } = validation.validate({
|
|
3156
3164
|
firstName,
|
|
@@ -3160,7 +3168,7 @@ function useUserController() {
|
|
|
3160
3168
|
type
|
|
3161
3169
|
});
|
|
3162
3170
|
if (error) {
|
|
3163
|
-
next(new
|
|
3171
|
+
next(new BadRequestError13(error.message));
|
|
3164
3172
|
return;
|
|
3165
3173
|
}
|
|
3166
3174
|
try {
|
|
@@ -3193,43 +3201,43 @@ function useUserController() {
|
|
|
3193
3201
|
}
|
|
3194
3202
|
|
|
3195
3203
|
// src/resources/organization/organization.model.ts
|
|
3196
|
-
import { BadRequestError as
|
|
3197
|
-
import
|
|
3204
|
+
import { BadRequestError as BadRequestError14 } from "@eeplatform/nodejs-utils";
|
|
3205
|
+
import Joi5 from "joi";
|
|
3198
3206
|
import { ObjectId as ObjectId12 } from "mongodb";
|
|
3199
|
-
var schemaOrg =
|
|
3200
|
-
_id:
|
|
3201
|
-
name:
|
|
3202
|
-
email:
|
|
3203
|
-
contact:
|
|
3204
|
-
type:
|
|
3205
|
-
sector:
|
|
3206
|
-
sectorName:
|
|
3207
|
-
sectorType:
|
|
3208
|
-
category:
|
|
3209
|
-
region:
|
|
3210
|
-
regionName:
|
|
3211
|
-
province:
|
|
3212
|
-
provinceName:
|
|
3213
|
-
cityMunicipality:
|
|
3214
|
-
cityMunicipalityName:
|
|
3215
|
-
barangay:
|
|
3216
|
-
barangayName:
|
|
3217
|
-
description:
|
|
3218
|
-
status:
|
|
3219
|
-
createdAt:
|
|
3220
|
-
updatedAt:
|
|
3221
|
-
deletedAt:
|
|
3207
|
+
var schemaOrg = Joi5.object({
|
|
3208
|
+
_id: Joi5.string().hex().optional().allow("", null),
|
|
3209
|
+
name: Joi5.string().required(),
|
|
3210
|
+
email: Joi5.string().email().required(),
|
|
3211
|
+
contact: Joi5.string().required(),
|
|
3212
|
+
type: Joi5.string().required(),
|
|
3213
|
+
sector: Joi5.string().required(),
|
|
3214
|
+
sectorName: Joi5.string().optional().allow("", null),
|
|
3215
|
+
sectorType: Joi5.string().allow("public", "private").required(),
|
|
3216
|
+
category: Joi5.string().required(),
|
|
3217
|
+
region: Joi5.string().optional().allow("", null),
|
|
3218
|
+
regionName: Joi5.string().optional().allow("", null),
|
|
3219
|
+
province: Joi5.string().optional().allow("", null),
|
|
3220
|
+
provinceName: Joi5.string().optional().allow("", null),
|
|
3221
|
+
cityMunicipality: Joi5.string().optional().allow("", null),
|
|
3222
|
+
cityMunicipalityName: Joi5.string().optional().allow("", null),
|
|
3223
|
+
barangay: Joi5.string().optional().allow("", null),
|
|
3224
|
+
barangayName: Joi5.string().optional().allow("", null),
|
|
3225
|
+
description: Joi5.string().optional().allow("", null),
|
|
3226
|
+
status: Joi5.string().optional().allow("", null),
|
|
3227
|
+
createdAt: Joi5.string().optional().allow("", null),
|
|
3228
|
+
updatedAt: Joi5.string().optional().allow("", null),
|
|
3229
|
+
deletedAt: Joi5.string().optional().allow("", null)
|
|
3222
3230
|
});
|
|
3223
3231
|
function MOrg(value) {
|
|
3224
3232
|
const { error } = schemaOrg.validate(value);
|
|
3225
3233
|
if (error) {
|
|
3226
|
-
throw new
|
|
3234
|
+
throw new BadRequestError14(error.message);
|
|
3227
3235
|
}
|
|
3228
3236
|
if (value._id) {
|
|
3229
3237
|
try {
|
|
3230
3238
|
value._id = new ObjectId12(value._id);
|
|
3231
3239
|
} catch (error2) {
|
|
3232
|
-
throw new
|
|
3240
|
+
throw new BadRequestError14("Invalid ID.");
|
|
3233
3241
|
}
|
|
3234
3242
|
}
|
|
3235
3243
|
return {
|
|
@@ -3260,24 +3268,24 @@ function MOrg(value) {
|
|
|
3260
3268
|
|
|
3261
3269
|
// src/resources/organization/organization.repository.ts
|
|
3262
3270
|
import {
|
|
3263
|
-
AppError as
|
|
3264
|
-
BadRequestError as
|
|
3271
|
+
AppError as AppError6,
|
|
3272
|
+
BadRequestError as BadRequestError15,
|
|
3265
3273
|
InternalServerError as InternalServerError12,
|
|
3266
3274
|
logger as logger10,
|
|
3267
3275
|
makeCacheKey as makeCacheKey8,
|
|
3268
3276
|
paginate as paginate5,
|
|
3269
|
-
useAtlas as
|
|
3270
|
-
useCache as
|
|
3277
|
+
useAtlas as useAtlas8,
|
|
3278
|
+
useCache as useCache8
|
|
3271
3279
|
} from "@eeplatform/nodejs-utils";
|
|
3272
3280
|
import { ObjectId as ObjectId13 } from "mongodb";
|
|
3273
3281
|
function useOrgRepo() {
|
|
3274
|
-
const db =
|
|
3282
|
+
const db = useAtlas8.getDb();
|
|
3275
3283
|
if (!db) {
|
|
3276
3284
|
throw new Error("Unable to connect to server.");
|
|
3277
3285
|
}
|
|
3278
3286
|
const namespace_collection = "organizations";
|
|
3279
3287
|
const collection = db.collection(namespace_collection);
|
|
3280
|
-
const { getCache, setCache, delNamespace } =
|
|
3288
|
+
const { getCache, setCache, delNamespace } = useCache8(namespace_collection);
|
|
3281
3289
|
async function createIndexes() {
|
|
3282
3290
|
try {
|
|
3283
3291
|
await collection.createIndexes([
|
|
@@ -3315,12 +3323,12 @@ function useOrgRepo() {
|
|
|
3315
3323
|
level: "error",
|
|
3316
3324
|
message: error.message
|
|
3317
3325
|
});
|
|
3318
|
-
if (error instanceof
|
|
3326
|
+
if (error instanceof AppError6) {
|
|
3319
3327
|
throw error;
|
|
3320
3328
|
} else {
|
|
3321
3329
|
const isDuplicated = error.message.includes("duplicate");
|
|
3322
3330
|
if (isDuplicated) {
|
|
3323
|
-
throw new
|
|
3331
|
+
throw new BadRequestError15("Organization already exist.");
|
|
3324
3332
|
}
|
|
3325
3333
|
throw new Error("Failed to create organization.");
|
|
3326
3334
|
}
|
|
@@ -3397,7 +3405,7 @@ function useOrgRepo() {
|
|
|
3397
3405
|
try {
|
|
3398
3406
|
_id = new ObjectId13(_id);
|
|
3399
3407
|
} catch (error) {
|
|
3400
|
-
throw new
|
|
3408
|
+
throw new BadRequestError15("Invalid ID.");
|
|
3401
3409
|
}
|
|
3402
3410
|
const cacheKey = makeCacheKey8(namespace_collection, { _id: String(_id) });
|
|
3403
3411
|
try {
|
|
@@ -3411,7 +3419,7 @@ function useOrgRepo() {
|
|
|
3411
3419
|
}
|
|
3412
3420
|
const result = await collection.findOne({ _id });
|
|
3413
3421
|
if (!result) {
|
|
3414
|
-
throw new
|
|
3422
|
+
throw new BadRequestError15("Organization not found.");
|
|
3415
3423
|
}
|
|
3416
3424
|
setCache(cacheKey, result, 300).then(() => {
|
|
3417
3425
|
logger10.log({
|
|
@@ -3426,7 +3434,7 @@ function useOrgRepo() {
|
|
|
3426
3434
|
});
|
|
3427
3435
|
return result;
|
|
3428
3436
|
} catch (error) {
|
|
3429
|
-
if (error instanceof
|
|
3437
|
+
if (error instanceof AppError6) {
|
|
3430
3438
|
throw error;
|
|
3431
3439
|
} else {
|
|
3432
3440
|
throw new InternalServerError12("Failed to get organization.");
|
|
@@ -3446,7 +3454,7 @@ function useOrgRepo() {
|
|
|
3446
3454
|
}
|
|
3447
3455
|
const result = await collection.findOne({ name });
|
|
3448
3456
|
if (!result) {
|
|
3449
|
-
throw new
|
|
3457
|
+
throw new BadRequestError15("Organization not found.");
|
|
3450
3458
|
}
|
|
3451
3459
|
setCache(cacheKey, result, 300).then(() => {
|
|
3452
3460
|
logger10.log({
|
|
@@ -3461,7 +3469,7 @@ function useOrgRepo() {
|
|
|
3461
3469
|
});
|
|
3462
3470
|
return result;
|
|
3463
3471
|
} catch (error) {
|
|
3464
|
-
if (error instanceof
|
|
3472
|
+
if (error instanceof AppError6) {
|
|
3465
3473
|
throw error;
|
|
3466
3474
|
} else {
|
|
3467
3475
|
throw new InternalServerError12("Failed to get organization.");
|
|
@@ -3471,14 +3479,14 @@ function useOrgRepo() {
|
|
|
3471
3479
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
3472
3480
|
const allowedFields = ["name", "description"];
|
|
3473
3481
|
if (!allowedFields.includes(field)) {
|
|
3474
|
-
throw new
|
|
3482
|
+
throw new BadRequestError15(
|
|
3475
3483
|
`Field "${field}" is not allowed to be updated.`
|
|
3476
3484
|
);
|
|
3477
3485
|
}
|
|
3478
3486
|
try {
|
|
3479
3487
|
_id = new ObjectId13(_id);
|
|
3480
3488
|
} catch (error) {
|
|
3481
|
-
throw new
|
|
3489
|
+
throw new BadRequestError15("Invalid ID.");
|
|
3482
3490
|
}
|
|
3483
3491
|
try {
|
|
3484
3492
|
await collection.updateOne(
|
|
@@ -3497,7 +3505,7 @@ function useOrgRepo() {
|
|
|
3497
3505
|
try {
|
|
3498
3506
|
_id = new ObjectId13(_id);
|
|
3499
3507
|
} catch (error) {
|
|
3500
|
-
throw new
|
|
3508
|
+
throw new BadRequestError15("Invalid ID.");
|
|
3501
3509
|
}
|
|
3502
3510
|
try {
|
|
3503
3511
|
await collection.updateOne(
|
|
@@ -3522,18 +3530,19 @@ function useOrgRepo() {
|
|
|
3522
3530
|
}
|
|
3523
3531
|
|
|
3524
3532
|
// src/resources/organization/organization.service.ts
|
|
3525
|
-
import { useAtlas as
|
|
3533
|
+
import { useAtlas as useAtlas9 } from "@eeplatform/nodejs-utils";
|
|
3526
3534
|
function useOrgService() {
|
|
3527
3535
|
const { add: addOrg } = useOrgRepo();
|
|
3528
3536
|
const { addRole } = useRoleRepo();
|
|
3529
3537
|
async function add(value) {
|
|
3530
|
-
const session =
|
|
3538
|
+
const session = useAtlas9.getClient()?.startSession();
|
|
3531
3539
|
session?.startTransaction();
|
|
3532
3540
|
try {
|
|
3533
3541
|
const org = await addOrg(value, session);
|
|
3534
3542
|
const role = await addRole(
|
|
3535
3543
|
{
|
|
3536
|
-
|
|
3544
|
+
app: "org",
|
|
3545
|
+
org,
|
|
3537
3546
|
name: "Owner",
|
|
3538
3547
|
description: "Owner of the organization",
|
|
3539
3548
|
permissions: ["all"]
|
|
@@ -3555,8 +3564,8 @@ function useOrgService() {
|
|
|
3555
3564
|
}
|
|
3556
3565
|
|
|
3557
3566
|
// src/resources/organization/organization.controller.ts
|
|
3558
|
-
import { BadRequestError as
|
|
3559
|
-
import
|
|
3567
|
+
import { BadRequestError as BadRequestError16 } from "@eeplatform/nodejs-utils";
|
|
3568
|
+
import Joi6 from "joi";
|
|
3560
3569
|
function useOrgController() {
|
|
3561
3570
|
const { add: _add } = useOrgService();
|
|
3562
3571
|
const { getOrgsByMembership } = useMemberRepo();
|
|
@@ -3569,7 +3578,7 @@ function useOrgController() {
|
|
|
3569
3578
|
const value = req.body;
|
|
3570
3579
|
const { error } = schemaOrg.validate(value);
|
|
3571
3580
|
if (error) {
|
|
3572
|
-
next(new
|
|
3581
|
+
next(new BadRequestError16(error.message));
|
|
3573
3582
|
return;
|
|
3574
3583
|
}
|
|
3575
3584
|
try {
|
|
@@ -3587,23 +3596,23 @@ function useOrgController() {
|
|
|
3587
3596
|
const user = req.params.user ?? "";
|
|
3588
3597
|
const isPageNumber = isFinite(page);
|
|
3589
3598
|
if (!isPageNumber) {
|
|
3590
|
-
next(new
|
|
3599
|
+
next(new BadRequestError16("Invalid page number."));
|
|
3591
3600
|
return;
|
|
3592
3601
|
}
|
|
3593
3602
|
const isLimitNumber = isFinite(limit);
|
|
3594
3603
|
if (!isLimitNumber) {
|
|
3595
|
-
next(new
|
|
3604
|
+
next(new BadRequestError16("Invalid limit number."));
|
|
3596
3605
|
return;
|
|
3597
3606
|
}
|
|
3598
|
-
const validation =
|
|
3599
|
-
user:
|
|
3600
|
-
page:
|
|
3601
|
-
limit:
|
|
3602
|
-
search:
|
|
3607
|
+
const validation = Joi6.object({
|
|
3608
|
+
user: Joi6.string().hex().required(),
|
|
3609
|
+
page: Joi6.number().min(1).optional().allow("", null),
|
|
3610
|
+
limit: Joi6.number().min(1).optional().allow("", null),
|
|
3611
|
+
search: Joi6.string().optional().allow("", null)
|
|
3603
3612
|
});
|
|
3604
3613
|
const { error } = validation.validate({ user, page, limit, search });
|
|
3605
3614
|
if (error) {
|
|
3606
|
-
next(new
|
|
3615
|
+
next(new BadRequestError16(error.message));
|
|
3607
3616
|
return;
|
|
3608
3617
|
}
|
|
3609
3618
|
try {
|
|
@@ -3616,11 +3625,11 @@ function useOrgController() {
|
|
|
3616
3625
|
}
|
|
3617
3626
|
async function getAll(req, res, next) {
|
|
3618
3627
|
const query = req.query;
|
|
3619
|
-
const validation =
|
|
3620
|
-
page:
|
|
3621
|
-
limit:
|
|
3622
|
-
search:
|
|
3623
|
-
status:
|
|
3628
|
+
const validation = Joi6.object({
|
|
3629
|
+
page: Joi6.number().min(1).optional().allow("", null),
|
|
3630
|
+
limit: Joi6.number().min(1).optional().allow("", null),
|
|
3631
|
+
search: Joi6.string().optional().allow("", null),
|
|
3632
|
+
status: Joi6.string().valid("active", "suspended", "inactive", "deleted").optional()
|
|
3624
3633
|
});
|
|
3625
3634
|
const { error } = validation.validate(query);
|
|
3626
3635
|
const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
|
|
@@ -3629,16 +3638,16 @@ function useOrgController() {
|
|
|
3629
3638
|
const status = req.query.status ?? "active";
|
|
3630
3639
|
const isPageNumber = isFinite(page);
|
|
3631
3640
|
if (!isPageNumber) {
|
|
3632
|
-
next(new
|
|
3641
|
+
next(new BadRequestError16("Invalid page number."));
|
|
3633
3642
|
return;
|
|
3634
3643
|
}
|
|
3635
3644
|
const isLimitNumber = isFinite(limit);
|
|
3636
3645
|
if (!isLimitNumber) {
|
|
3637
|
-
next(new
|
|
3646
|
+
next(new BadRequestError16("Invalid limit number."));
|
|
3638
3647
|
return;
|
|
3639
3648
|
}
|
|
3640
3649
|
if (error) {
|
|
3641
|
-
next(new
|
|
3650
|
+
next(new BadRequestError16(error.message));
|
|
3642
3651
|
return;
|
|
3643
3652
|
}
|
|
3644
3653
|
try {
|
|
@@ -3651,12 +3660,12 @@ function useOrgController() {
|
|
|
3651
3660
|
}
|
|
3652
3661
|
async function getByName(req, res, next) {
|
|
3653
3662
|
const name = req.params.name;
|
|
3654
|
-
const validation =
|
|
3655
|
-
name:
|
|
3663
|
+
const validation = Joi6.object({
|
|
3664
|
+
name: Joi6.string().required()
|
|
3656
3665
|
});
|
|
3657
3666
|
const { error } = validation.validate({ name });
|
|
3658
3667
|
if (error) {
|
|
3659
|
-
next(new
|
|
3668
|
+
next(new BadRequestError16(error.message));
|
|
3660
3669
|
return;
|
|
3661
3670
|
}
|
|
3662
3671
|
try {
|
|
@@ -3669,12 +3678,12 @@ function useOrgController() {
|
|
|
3669
3678
|
}
|
|
3670
3679
|
async function getById(req, res, next) {
|
|
3671
3680
|
const id = req.params.id;
|
|
3672
|
-
const validation =
|
|
3673
|
-
id:
|
|
3681
|
+
const validation = Joi6.object({
|
|
3682
|
+
id: Joi6.string().hex().required()
|
|
3674
3683
|
});
|
|
3675
3684
|
const { error } = validation.validate({ id });
|
|
3676
3685
|
if (error) {
|
|
3677
|
-
next(new
|
|
3686
|
+
next(new BadRequestError16(error.message));
|
|
3678
3687
|
return;
|
|
3679
3688
|
}
|
|
3680
3689
|
try {
|
|
@@ -3695,23 +3704,23 @@ function useOrgController() {
|
|
|
3695
3704
|
}
|
|
3696
3705
|
|
|
3697
3706
|
// src/resources/app/app.model.ts
|
|
3698
|
-
import { BadRequestError as
|
|
3699
|
-
import
|
|
3700
|
-
var schemaApp =
|
|
3701
|
-
code:
|
|
3702
|
-
name:
|
|
3703
|
-
description:
|
|
3704
|
-
type:
|
|
3707
|
+
import { BadRequestError as BadRequestError17 } from "@eeplatform/nodejs-utils";
|
|
3708
|
+
import Joi7 from "joi";
|
|
3709
|
+
var schemaApp = Joi7.object({
|
|
3710
|
+
code: Joi7.string().max(20).required(),
|
|
3711
|
+
name: Joi7.string().max(255).required(),
|
|
3712
|
+
description: Joi7.string().max(1024).optional().allow("", null),
|
|
3713
|
+
type: Joi7.string().allow("default", "standard").optional().allow("", null)
|
|
3705
3714
|
});
|
|
3706
|
-
var schemaAppUpdate =
|
|
3707
|
-
code:
|
|
3708
|
-
name:
|
|
3709
|
-
description:
|
|
3715
|
+
var schemaAppUpdate = Joi7.object({
|
|
3716
|
+
code: Joi7.string().max(20).optional().allow("", null),
|
|
3717
|
+
name: Joi7.string().max(255).optional().allow("", null),
|
|
3718
|
+
description: Joi7.string().max(1024).optional().allow("", null)
|
|
3710
3719
|
});
|
|
3711
3720
|
function modelApp(value) {
|
|
3712
3721
|
const { error } = schemaApp.validate(value);
|
|
3713
3722
|
if (error) {
|
|
3714
|
-
throw new
|
|
3723
|
+
throw new BadRequestError17(error.message);
|
|
3715
3724
|
}
|
|
3716
3725
|
return {
|
|
3717
3726
|
_id: value._id,
|
|
@@ -3728,19 +3737,19 @@ function modelApp(value) {
|
|
|
3728
3737
|
|
|
3729
3738
|
// src/resources/app/app.repository.ts
|
|
3730
3739
|
import {
|
|
3731
|
-
AppError as
|
|
3732
|
-
BadRequestError as
|
|
3740
|
+
AppError as AppError7,
|
|
3741
|
+
BadRequestError as BadRequestError18,
|
|
3733
3742
|
InternalServerError as InternalServerError13,
|
|
3734
3743
|
logger as logger11,
|
|
3735
3744
|
makeCacheKey as makeCacheKey9,
|
|
3736
3745
|
paginate as paginate6,
|
|
3737
|
-
useRepo
|
|
3746
|
+
useRepo as useRepo2
|
|
3738
3747
|
} from "@eeplatform/nodejs-utils";
|
|
3739
3748
|
import { ObjectId as ObjectId14 } from "mongodb";
|
|
3740
|
-
import
|
|
3749
|
+
import Joi8 from "joi";
|
|
3741
3750
|
function useAppRepo() {
|
|
3742
3751
|
const namespace_collection = "apps";
|
|
3743
|
-
const repo =
|
|
3752
|
+
const repo = useRepo2(namespace_collection);
|
|
3744
3753
|
async function createIndexes() {
|
|
3745
3754
|
try {
|
|
3746
3755
|
await repo.collection.createIndexes([
|
|
@@ -3778,12 +3787,12 @@ function useAppRepo() {
|
|
|
3778
3787
|
level: "error",
|
|
3779
3788
|
message: error.message
|
|
3780
3789
|
});
|
|
3781
|
-
if (error instanceof
|
|
3790
|
+
if (error instanceof AppError7) {
|
|
3782
3791
|
throw error;
|
|
3783
3792
|
} else {
|
|
3784
3793
|
const isDuplicated = error.message.includes("duplicate");
|
|
3785
3794
|
if (isDuplicated) {
|
|
3786
|
-
throw new
|
|
3795
|
+
throw new BadRequestError18("App already exists.");
|
|
3787
3796
|
}
|
|
3788
3797
|
throw new Error("Failed to create app.");
|
|
3789
3798
|
}
|
|
@@ -3793,7 +3802,7 @@ function useAppRepo() {
|
|
|
3793
3802
|
try {
|
|
3794
3803
|
_id = new ObjectId14(_id);
|
|
3795
3804
|
} catch (error) {
|
|
3796
|
-
throw new
|
|
3805
|
+
throw new BadRequestError18("Invalid ID.");
|
|
3797
3806
|
}
|
|
3798
3807
|
try {
|
|
3799
3808
|
const res = await repo.collection.updateOne(
|
|
@@ -3808,7 +3817,7 @@ function useAppRepo() {
|
|
|
3808
3817
|
level: "error",
|
|
3809
3818
|
message: error.message
|
|
3810
3819
|
});
|
|
3811
|
-
if (error instanceof
|
|
3820
|
+
if (error instanceof AppError7) {
|
|
3812
3821
|
throw error;
|
|
3813
3822
|
} else {
|
|
3814
3823
|
throw new Error("Failed to update app.");
|
|
@@ -3889,7 +3898,7 @@ function useAppRepo() {
|
|
|
3889
3898
|
try {
|
|
3890
3899
|
_id = new ObjectId14(_id);
|
|
3891
3900
|
} catch (error) {
|
|
3892
|
-
throw new
|
|
3901
|
+
throw new BadRequestError18("Invalid ID.");
|
|
3893
3902
|
}
|
|
3894
3903
|
const cacheKey = makeCacheKey9(namespace_collection, { _id: String(_id) });
|
|
3895
3904
|
try {
|
|
@@ -3917,7 +3926,7 @@ function useAppRepo() {
|
|
|
3917
3926
|
});
|
|
3918
3927
|
return result;
|
|
3919
3928
|
} catch (error) {
|
|
3920
|
-
if (error instanceof
|
|
3929
|
+
if (error instanceof AppError7) {
|
|
3921
3930
|
throw error;
|
|
3922
3931
|
} else {
|
|
3923
3932
|
throw new InternalServerError13("Failed to get app.");
|
|
@@ -3925,10 +3934,10 @@ function useAppRepo() {
|
|
|
3925
3934
|
}
|
|
3926
3935
|
}
|
|
3927
3936
|
async function getByCode(code) {
|
|
3928
|
-
const validate =
|
|
3937
|
+
const validate = Joi8.string().required();
|
|
3929
3938
|
const { error } = validate.validate(code);
|
|
3930
3939
|
if (error) {
|
|
3931
|
-
throw new
|
|
3940
|
+
throw new BadRequestError18("Invalid code.");
|
|
3932
3941
|
}
|
|
3933
3942
|
const cacheKey = makeCacheKey9(namespace_collection, {
|
|
3934
3943
|
code,
|
|
@@ -3959,7 +3968,7 @@ function useAppRepo() {
|
|
|
3959
3968
|
});
|
|
3960
3969
|
return result;
|
|
3961
3970
|
} catch (error2) {
|
|
3962
|
-
if (error2 instanceof
|
|
3971
|
+
if (error2 instanceof AppError7) {
|
|
3963
3972
|
throw error2;
|
|
3964
3973
|
} else {
|
|
3965
3974
|
throw new InternalServerError13("Failed to get app.");
|
|
@@ -3970,7 +3979,7 @@ function useAppRepo() {
|
|
|
3970
3979
|
try {
|
|
3971
3980
|
_id = new ObjectId14(_id);
|
|
3972
3981
|
} catch (error) {
|
|
3973
|
-
throw new
|
|
3982
|
+
throw new BadRequestError18("Invalid ID.");
|
|
3974
3983
|
}
|
|
3975
3984
|
try {
|
|
3976
3985
|
const res = await repo.collection.updateOne(
|
|
@@ -3984,7 +3993,7 @@ function useAppRepo() {
|
|
|
3984
3993
|
level: "error",
|
|
3985
3994
|
message: error.message
|
|
3986
3995
|
});
|
|
3987
|
-
if (error instanceof
|
|
3996
|
+
if (error instanceof AppError7) {
|
|
3988
3997
|
throw error;
|
|
3989
3998
|
} else {
|
|
3990
3999
|
throw new InternalServerError13("Failed to delete app.");
|
|
@@ -4003,7 +4012,7 @@ function useAppRepo() {
|
|
|
4003
4012
|
}
|
|
4004
4013
|
|
|
4005
4014
|
// src/resources/app/app.service.ts
|
|
4006
|
-
import { logger as logger12, useAtlas as
|
|
4015
|
+
import { logger as logger12, useAtlas as useAtlas10 } from "@eeplatform/nodejs-utils";
|
|
4007
4016
|
function useAppService() {
|
|
4008
4017
|
const {
|
|
4009
4018
|
updateById: _updateById,
|
|
@@ -4036,7 +4045,7 @@ function useAppService() {
|
|
|
4036
4045
|
description: "Basic Education School application."
|
|
4037
4046
|
}
|
|
4038
4047
|
];
|
|
4039
|
-
const session =
|
|
4048
|
+
const session = useAtlas10.getClient()?.startSession();
|
|
4040
4049
|
if (!session) {
|
|
4041
4050
|
throw new Error("Failed to start database session.");
|
|
4042
4051
|
}
|
|
@@ -4080,8 +4089,8 @@ function useAppService() {
|
|
|
4080
4089
|
}
|
|
4081
4090
|
|
|
4082
4091
|
// src/resources/app/app.controller.ts
|
|
4083
|
-
import { BadRequestError as
|
|
4084
|
-
import
|
|
4092
|
+
import { BadRequestError as BadRequestError19 } from "@eeplatform/nodejs-utils";
|
|
4093
|
+
import Joi9 from "joi";
|
|
4085
4094
|
function useAppController() {
|
|
4086
4095
|
const {
|
|
4087
4096
|
getAll: _getAll,
|
|
@@ -4094,7 +4103,7 @@ function useAppController() {
|
|
|
4094
4103
|
const value = req.body;
|
|
4095
4104
|
const { error } = schemaApp.validate(value);
|
|
4096
4105
|
if (error) {
|
|
4097
|
-
next(new
|
|
4106
|
+
next(new BadRequestError19(error.message));
|
|
4098
4107
|
return;
|
|
4099
4108
|
}
|
|
4100
4109
|
try {
|
|
@@ -4107,15 +4116,15 @@ function useAppController() {
|
|
|
4107
4116
|
}
|
|
4108
4117
|
async function updateById(req, res, next) {
|
|
4109
4118
|
const id = req.params.id ?? "";
|
|
4110
|
-
const { error: errorId } =
|
|
4119
|
+
const { error: errorId } = Joi9.string().hex().required().validate(id);
|
|
4111
4120
|
if (errorId) {
|
|
4112
|
-
next(new
|
|
4121
|
+
next(new BadRequestError19(errorId.message));
|
|
4113
4122
|
return;
|
|
4114
4123
|
}
|
|
4115
4124
|
const value = req.body;
|
|
4116
4125
|
const { error } = schemaAppUpdate.validate(value);
|
|
4117
4126
|
if (error) {
|
|
4118
|
-
next(new
|
|
4127
|
+
next(new BadRequestError19(error.message));
|
|
4119
4128
|
return;
|
|
4120
4129
|
}
|
|
4121
4130
|
try {
|
|
@@ -4128,16 +4137,16 @@ function useAppController() {
|
|
|
4128
4137
|
}
|
|
4129
4138
|
async function getAll(req, res, next) {
|
|
4130
4139
|
const query = req.query;
|
|
4131
|
-
const validation =
|
|
4132
|
-
page:
|
|
4133
|
-
limit:
|
|
4134
|
-
search:
|
|
4135
|
-
status:
|
|
4136
|
-
type:
|
|
4140
|
+
const validation = Joi9.object({
|
|
4141
|
+
page: Joi9.number().min(1).optional().allow("", null),
|
|
4142
|
+
limit: Joi9.number().min(1).optional().allow("", null),
|
|
4143
|
+
search: Joi9.string().optional().allow("", null),
|
|
4144
|
+
status: Joi9.string().optional().allow("", null),
|
|
4145
|
+
type: Joi9.string().optional().allow("", null)
|
|
4137
4146
|
});
|
|
4138
4147
|
const { error } = validation.validate(query);
|
|
4139
4148
|
if (error) {
|
|
4140
|
-
next(new
|
|
4149
|
+
next(new BadRequestError19(error.message));
|
|
4141
4150
|
return;
|
|
4142
4151
|
}
|
|
4143
4152
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -4171,12 +4180,12 @@ function useAppController() {
|
|
|
4171
4180
|
}
|
|
4172
4181
|
async function getById(req, res, next) {
|
|
4173
4182
|
const id = req.params.id;
|
|
4174
|
-
const validation =
|
|
4175
|
-
id:
|
|
4183
|
+
const validation = Joi9.object({
|
|
4184
|
+
id: Joi9.string().hex().required()
|
|
4176
4185
|
});
|
|
4177
4186
|
const { error } = validation.validate({ id });
|
|
4178
4187
|
if (error) {
|
|
4179
|
-
next(new
|
|
4188
|
+
next(new BadRequestError19(error.message));
|
|
4180
4189
|
return;
|
|
4181
4190
|
}
|
|
4182
4191
|
try {
|
|
@@ -4192,12 +4201,12 @@ function useAppController() {
|
|
|
4192
4201
|
}
|
|
4193
4202
|
async function deleteById(req, res, next) {
|
|
4194
4203
|
const id = req.params.id;
|
|
4195
|
-
const validation =
|
|
4196
|
-
id:
|
|
4204
|
+
const validation = Joi9.object({
|
|
4205
|
+
id: Joi9.string().hex().required()
|
|
4197
4206
|
});
|
|
4198
4207
|
const { error } = validation.validate({ id });
|
|
4199
4208
|
if (error) {
|
|
4200
|
-
next(new
|
|
4209
|
+
next(new BadRequestError19(error.message));
|
|
4201
4210
|
return;
|
|
4202
4211
|
}
|
|
4203
4212
|
try {
|
|
@@ -4218,26 +4227,26 @@ function useAppController() {
|
|
|
4218
4227
|
}
|
|
4219
4228
|
|
|
4220
4229
|
// src/resources/permission/permission.model.ts
|
|
4221
|
-
import { BadRequestError as
|
|
4222
|
-
import
|
|
4223
|
-
var schemaPermission =
|
|
4224
|
-
app:
|
|
4225
|
-
key:
|
|
4226
|
-
name:
|
|
4227
|
-
group:
|
|
4228
|
-
description:
|
|
4229
|
-
deprecated:
|
|
4230
|
+
import { BadRequestError as BadRequestError20 } from "@eeplatform/nodejs-utils";
|
|
4231
|
+
import Joi10 from "joi";
|
|
4232
|
+
var schemaPermission = Joi10.object({
|
|
4233
|
+
app: Joi10.string().required(),
|
|
4234
|
+
key: Joi10.string().required(),
|
|
4235
|
+
name: Joi10.string().required(),
|
|
4236
|
+
group: Joi10.string().required(),
|
|
4237
|
+
description: Joi10.string().required(),
|
|
4238
|
+
deprecated: Joi10.boolean().optional().allow(null)
|
|
4230
4239
|
});
|
|
4231
|
-
var schemaPermissionUpdate =
|
|
4232
|
-
key:
|
|
4233
|
-
name:
|
|
4234
|
-
group:
|
|
4235
|
-
description:
|
|
4240
|
+
var schemaPermissionUpdate = Joi10.object({
|
|
4241
|
+
key: Joi10.string().optional().allow("", null),
|
|
4242
|
+
name: Joi10.string().optional().allow("", null),
|
|
4243
|
+
group: Joi10.string().optional().allow("", null),
|
|
4244
|
+
description: Joi10.string().max(1024).optional().allow("", null)
|
|
4236
4245
|
});
|
|
4237
4246
|
function modelPermission(value) {
|
|
4238
4247
|
const { error } = schemaPermission.validate(value);
|
|
4239
4248
|
if (error) {
|
|
4240
|
-
throw new
|
|
4249
|
+
throw new BadRequestError20(error.message);
|
|
4241
4250
|
}
|
|
4242
4251
|
return {
|
|
4243
4252
|
_id: value._id,
|
|
@@ -4256,19 +4265,19 @@ function modelPermission(value) {
|
|
|
4256
4265
|
|
|
4257
4266
|
// src/resources/permission/permission.repository.ts
|
|
4258
4267
|
import {
|
|
4259
|
-
AppError as
|
|
4260
|
-
BadRequestError as
|
|
4268
|
+
AppError as AppError8,
|
|
4269
|
+
BadRequestError as BadRequestError21,
|
|
4261
4270
|
InternalServerError as InternalServerError14,
|
|
4262
4271
|
logger as logger14,
|
|
4263
4272
|
makeCacheKey as makeCacheKey10,
|
|
4264
4273
|
paginate as paginate7,
|
|
4265
|
-
useRepo as
|
|
4274
|
+
useRepo as useRepo3
|
|
4266
4275
|
} from "@eeplatform/nodejs-utils";
|
|
4267
4276
|
import { ObjectId as ObjectId15 } from "mongodb";
|
|
4268
|
-
import
|
|
4277
|
+
import Joi11 from "joi";
|
|
4269
4278
|
function usePermissionRepo() {
|
|
4270
4279
|
const namespace_collection = "permissions";
|
|
4271
|
-
const repo =
|
|
4280
|
+
const repo = useRepo3(namespace_collection);
|
|
4272
4281
|
async function createIndexes() {
|
|
4273
4282
|
try {
|
|
4274
4283
|
await repo.collection.createIndexes([
|
|
@@ -4307,12 +4316,12 @@ function usePermissionRepo() {
|
|
|
4307
4316
|
level: "error",
|
|
4308
4317
|
message: error.message
|
|
4309
4318
|
});
|
|
4310
|
-
if (error instanceof
|
|
4319
|
+
if (error instanceof AppError8) {
|
|
4311
4320
|
throw error;
|
|
4312
4321
|
} else {
|
|
4313
4322
|
const isDuplicated = error.message.includes("duplicate");
|
|
4314
4323
|
if (isDuplicated) {
|
|
4315
|
-
throw new
|
|
4324
|
+
throw new BadRequestError21("Permission already exists.");
|
|
4316
4325
|
}
|
|
4317
4326
|
throw new Error("Failed to create permission.");
|
|
4318
4327
|
}
|
|
@@ -4322,11 +4331,11 @@ function usePermissionRepo() {
|
|
|
4322
4331
|
try {
|
|
4323
4332
|
_id = new ObjectId15(_id);
|
|
4324
4333
|
} catch (error2) {
|
|
4325
|
-
throw new
|
|
4334
|
+
throw new BadRequestError21("Invalid ID.");
|
|
4326
4335
|
}
|
|
4327
4336
|
const { error } = schemaPermissionUpdate.validate(value);
|
|
4328
4337
|
if (error) {
|
|
4329
|
-
throw new
|
|
4338
|
+
throw new BadRequestError21(`Invalid data: ${error.message}`);
|
|
4330
4339
|
}
|
|
4331
4340
|
try {
|
|
4332
4341
|
const res = await repo.collection.updateOne(
|
|
@@ -4341,7 +4350,7 @@ function usePermissionRepo() {
|
|
|
4341
4350
|
level: "error",
|
|
4342
4351
|
message: error2.message
|
|
4343
4352
|
});
|
|
4344
|
-
if (error2 instanceof
|
|
4353
|
+
if (error2 instanceof AppError8) {
|
|
4345
4354
|
throw error2;
|
|
4346
4355
|
} else {
|
|
4347
4356
|
throw new Error("Failed to update permission.");
|
|
@@ -4417,7 +4426,7 @@ function usePermissionRepo() {
|
|
|
4417
4426
|
try {
|
|
4418
4427
|
_id = new ObjectId15(_id);
|
|
4419
4428
|
} catch (error) {
|
|
4420
|
-
throw new
|
|
4429
|
+
throw new BadRequestError21("Invalid ID.");
|
|
4421
4430
|
}
|
|
4422
4431
|
const cacheKey = makeCacheKey10(namespace_collection, { _id: String(_id) });
|
|
4423
4432
|
try {
|
|
@@ -4445,7 +4454,7 @@ function usePermissionRepo() {
|
|
|
4445
4454
|
});
|
|
4446
4455
|
return result;
|
|
4447
4456
|
} catch (error) {
|
|
4448
|
-
if (error instanceof
|
|
4457
|
+
if (error instanceof AppError8) {
|
|
4449
4458
|
throw error;
|
|
4450
4459
|
} else {
|
|
4451
4460
|
throw new InternalServerError14("Failed to get permission.");
|
|
@@ -4453,14 +4462,14 @@ function usePermissionRepo() {
|
|
|
4453
4462
|
}
|
|
4454
4463
|
}
|
|
4455
4464
|
async function getByKey(key, group, app) {
|
|
4456
|
-
const validation =
|
|
4457
|
-
key:
|
|
4458
|
-
group:
|
|
4459
|
-
app:
|
|
4465
|
+
const validation = Joi11.object({
|
|
4466
|
+
key: Joi11.string().required(),
|
|
4467
|
+
group: Joi11.string().optional().allow("", null),
|
|
4468
|
+
app: Joi11.string().optional().allow("", null)
|
|
4460
4469
|
});
|
|
4461
4470
|
const { error } = validation.validate({ key, group });
|
|
4462
4471
|
if (error) {
|
|
4463
|
-
throw new
|
|
4472
|
+
throw new BadRequestError21(`Invalid data: ${error.message}`);
|
|
4464
4473
|
}
|
|
4465
4474
|
const query = {};
|
|
4466
4475
|
const cacheKeyOptions = {};
|
|
@@ -4498,7 +4507,7 @@ function usePermissionRepo() {
|
|
|
4498
4507
|
});
|
|
4499
4508
|
return result;
|
|
4500
4509
|
} catch (error2) {
|
|
4501
|
-
if (error2 instanceof
|
|
4510
|
+
if (error2 instanceof AppError8) {
|
|
4502
4511
|
throw error2;
|
|
4503
4512
|
} else {
|
|
4504
4513
|
throw new InternalServerError14("Failed to get permission.");
|
|
@@ -4535,7 +4544,7 @@ function usePermissionRepo() {
|
|
|
4535
4544
|
});
|
|
4536
4545
|
return result;
|
|
4537
4546
|
} catch (error) {
|
|
4538
|
-
if (error instanceof
|
|
4547
|
+
if (error instanceof AppError8) {
|
|
4539
4548
|
throw error;
|
|
4540
4549
|
} else {
|
|
4541
4550
|
throw new InternalServerError14("Failed to count permission by group.");
|
|
@@ -4546,7 +4555,7 @@ function usePermissionRepo() {
|
|
|
4546
4555
|
try {
|
|
4547
4556
|
_id = new ObjectId15(_id);
|
|
4548
4557
|
} catch (error) {
|
|
4549
|
-
throw new
|
|
4558
|
+
throw new BadRequestError21("Invalid ID.");
|
|
4550
4559
|
}
|
|
4551
4560
|
try {
|
|
4552
4561
|
const res = await repo.collection.updateOne(
|
|
@@ -4560,7 +4569,7 @@ function usePermissionRepo() {
|
|
|
4560
4569
|
level: "error",
|
|
4561
4570
|
message: error.message
|
|
4562
4571
|
});
|
|
4563
|
-
if (error instanceof
|
|
4572
|
+
if (error instanceof AppError8) {
|
|
4564
4573
|
throw error;
|
|
4565
4574
|
} else {
|
|
4566
4575
|
throw new InternalServerError14("Failed to delete permission.");
|
|
@@ -4600,8 +4609,8 @@ function usePermissionService() {
|
|
|
4600
4609
|
}
|
|
4601
4610
|
|
|
4602
4611
|
// src/resources/permission/permission.controller.ts
|
|
4603
|
-
import { BadRequestError as
|
|
4604
|
-
import
|
|
4612
|
+
import { BadRequestError as BadRequestError22, logger as logger15 } from "@eeplatform/nodejs-utils";
|
|
4613
|
+
import Joi12 from "joi";
|
|
4605
4614
|
function usePermissionController() {
|
|
4606
4615
|
const {
|
|
4607
4616
|
getAll: _getAll,
|
|
@@ -4614,7 +4623,7 @@ function usePermissionController() {
|
|
|
4614
4623
|
const value = req.body;
|
|
4615
4624
|
const { error } = schemaPermission.validate(value);
|
|
4616
4625
|
if (error) {
|
|
4617
|
-
next(new
|
|
4626
|
+
next(new BadRequestError22(error.message));
|
|
4618
4627
|
logger15.info(`Controller: ${error.message}`);
|
|
4619
4628
|
return;
|
|
4620
4629
|
}
|
|
@@ -4628,16 +4637,16 @@ function usePermissionController() {
|
|
|
4628
4637
|
}
|
|
4629
4638
|
async function getAll(req, res, next) {
|
|
4630
4639
|
const query = req.query;
|
|
4631
|
-
const validation =
|
|
4632
|
-
page:
|
|
4633
|
-
limit:
|
|
4634
|
-
search:
|
|
4635
|
-
app:
|
|
4636
|
-
status:
|
|
4640
|
+
const validation = Joi12.object({
|
|
4641
|
+
page: Joi12.number().min(1).optional().allow("", null),
|
|
4642
|
+
limit: Joi12.number().min(1).optional().allow("", null),
|
|
4643
|
+
search: Joi12.string().optional().allow("", null),
|
|
4644
|
+
app: Joi12.string().optional().allow("", null),
|
|
4645
|
+
status: Joi12.string().optional().allow("", null)
|
|
4637
4646
|
});
|
|
4638
4647
|
const { error } = validation.validate(query);
|
|
4639
4648
|
if (error) {
|
|
4640
|
-
next(new
|
|
4649
|
+
next(new BadRequestError22(error.message));
|
|
4641
4650
|
return;
|
|
4642
4651
|
}
|
|
4643
4652
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -4671,12 +4680,12 @@ function usePermissionController() {
|
|
|
4671
4680
|
}
|
|
4672
4681
|
async function getById(req, res, next) {
|
|
4673
4682
|
const id = req.params.id;
|
|
4674
|
-
const validation =
|
|
4675
|
-
id:
|
|
4683
|
+
const validation = Joi12.object({
|
|
4684
|
+
id: Joi12.string().hex().required()
|
|
4676
4685
|
});
|
|
4677
4686
|
const { error } = validation.validate({ id });
|
|
4678
4687
|
if (error) {
|
|
4679
|
-
next(new
|
|
4688
|
+
next(new BadRequestError22(error.message));
|
|
4680
4689
|
return;
|
|
4681
4690
|
}
|
|
4682
4691
|
try {
|
|
@@ -4692,12 +4701,12 @@ function usePermissionController() {
|
|
|
4692
4701
|
}
|
|
4693
4702
|
async function deleteById(req, res, next) {
|
|
4694
4703
|
const id = req.params.id;
|
|
4695
|
-
const validation =
|
|
4696
|
-
id:
|
|
4704
|
+
const validation = Joi12.object({
|
|
4705
|
+
id: Joi12.string().hex().required()
|
|
4697
4706
|
});
|
|
4698
4707
|
const { error } = validation.validate({ id });
|
|
4699
4708
|
if (error) {
|
|
4700
|
-
next(new
|
|
4709
|
+
next(new BadRequestError22(error.message));
|
|
4701
4710
|
return;
|
|
4702
4711
|
}
|
|
4703
4712
|
try {
|
|
@@ -4710,15 +4719,15 @@ function usePermissionController() {
|
|
|
4710
4719
|
}
|
|
4711
4720
|
async function updateById(req, res, next) {
|
|
4712
4721
|
const id = req.params.id;
|
|
4713
|
-
const { error: errorId } =
|
|
4722
|
+
const { error: errorId } = Joi12.string().hex().required().validate(id);
|
|
4714
4723
|
if (errorId) {
|
|
4715
|
-
next(new
|
|
4724
|
+
next(new BadRequestError22(errorId.message));
|
|
4716
4725
|
return;
|
|
4717
4726
|
}
|
|
4718
4727
|
const payload = req.body;
|
|
4719
4728
|
const { error } = schemaPermissionUpdate.validate(payload);
|
|
4720
4729
|
if (error) {
|
|
4721
|
-
next(new
|
|
4730
|
+
next(new BadRequestError22(error.message));
|
|
4722
4731
|
return;
|
|
4723
4732
|
}
|
|
4724
4733
|
try {
|
|
@@ -4739,23 +4748,23 @@ function usePermissionController() {
|
|
|
4739
4748
|
}
|
|
4740
4749
|
|
|
4741
4750
|
// src/resources/permission/permission.group.model.ts
|
|
4742
|
-
import { BadRequestError as
|
|
4743
|
-
import
|
|
4744
|
-
var schemaPermissionGroup =
|
|
4745
|
-
app:
|
|
4746
|
-
key:
|
|
4747
|
-
label:
|
|
4748
|
-
order:
|
|
4751
|
+
import { BadRequestError as BadRequestError23 } from "@eeplatform/nodejs-utils";
|
|
4752
|
+
import Joi13 from "joi";
|
|
4753
|
+
var schemaPermissionGroup = Joi13.object({
|
|
4754
|
+
app: Joi13.string().required(),
|
|
4755
|
+
key: Joi13.string().required(),
|
|
4756
|
+
label: Joi13.string().required(),
|
|
4757
|
+
order: Joi13.number().integer().optional().allow("", null)
|
|
4749
4758
|
});
|
|
4750
|
-
var schemaPermissionGroupUpdate =
|
|
4751
|
-
key:
|
|
4752
|
-
label:
|
|
4753
|
-
order:
|
|
4759
|
+
var schemaPermissionGroupUpdate = Joi13.object({
|
|
4760
|
+
key: Joi13.string().optional().allow("", null),
|
|
4761
|
+
label: Joi13.string().optional().allow("", null),
|
|
4762
|
+
order: Joi13.number().integer().optional().allow("", null)
|
|
4754
4763
|
});
|
|
4755
4764
|
function modelPermissionGroup(value) {
|
|
4756
4765
|
const { error } = schemaPermissionGroup.validate(value);
|
|
4757
4766
|
if (error) {
|
|
4758
|
-
throw new
|
|
4767
|
+
throw new BadRequestError23(error.message);
|
|
4759
4768
|
}
|
|
4760
4769
|
return {
|
|
4761
4770
|
_id: value._id,
|
|
@@ -4772,19 +4781,19 @@ function modelPermissionGroup(value) {
|
|
|
4772
4781
|
|
|
4773
4782
|
// src/resources/permission/permission.group.repository.ts
|
|
4774
4783
|
import {
|
|
4775
|
-
AppError as
|
|
4776
|
-
BadRequestError as
|
|
4784
|
+
AppError as AppError9,
|
|
4785
|
+
BadRequestError as BadRequestError24,
|
|
4777
4786
|
InternalServerError as InternalServerError15,
|
|
4778
4787
|
logger as logger16,
|
|
4779
4788
|
makeCacheKey as makeCacheKey11,
|
|
4780
4789
|
paginate as paginate8,
|
|
4781
|
-
useRepo as
|
|
4790
|
+
useRepo as useRepo4
|
|
4782
4791
|
} from "@eeplatform/nodejs-utils";
|
|
4783
4792
|
import { ObjectId as ObjectId16 } from "mongodb";
|
|
4784
|
-
import
|
|
4793
|
+
import Joi14 from "joi";
|
|
4785
4794
|
function usePermissionGroupRepo() {
|
|
4786
4795
|
const namespace_collection = "permission.groups";
|
|
4787
|
-
const repo =
|
|
4796
|
+
const repo = useRepo4(namespace_collection);
|
|
4788
4797
|
async function createIndexes() {
|
|
4789
4798
|
try {
|
|
4790
4799
|
await repo.collection.createIndexes([
|
|
@@ -4816,12 +4825,12 @@ function usePermissionGroupRepo() {
|
|
|
4816
4825
|
level: "error",
|
|
4817
4826
|
message: error.message
|
|
4818
4827
|
});
|
|
4819
|
-
if (error instanceof
|
|
4828
|
+
if (error instanceof AppError9) {
|
|
4820
4829
|
throw error;
|
|
4821
4830
|
} else {
|
|
4822
4831
|
const isDuplicated = error.message.includes("duplicate");
|
|
4823
4832
|
if (isDuplicated) {
|
|
4824
|
-
throw new
|
|
4833
|
+
throw new BadRequestError24("Permission group already exists.");
|
|
4825
4834
|
}
|
|
4826
4835
|
throw new Error("Failed to create permission group.");
|
|
4827
4836
|
}
|
|
@@ -4831,11 +4840,11 @@ function usePermissionGroupRepo() {
|
|
|
4831
4840
|
try {
|
|
4832
4841
|
_id = new ObjectId16(_id);
|
|
4833
4842
|
} catch (error2) {
|
|
4834
|
-
throw new
|
|
4843
|
+
throw new BadRequestError24("Invalid ID.");
|
|
4835
4844
|
}
|
|
4836
4845
|
const { error } = schemaPermissionGroupUpdate.validate(value);
|
|
4837
4846
|
if (error) {
|
|
4838
|
-
throw new
|
|
4847
|
+
throw new BadRequestError24(`Invalid data: ${error.message}`);
|
|
4839
4848
|
}
|
|
4840
4849
|
try {
|
|
4841
4850
|
const res = await repo.collection.updateOne(
|
|
@@ -4850,7 +4859,7 @@ function usePermissionGroupRepo() {
|
|
|
4850
4859
|
level: "error",
|
|
4851
4860
|
message: error2.message
|
|
4852
4861
|
});
|
|
4853
|
-
if (error2 instanceof
|
|
4862
|
+
if (error2 instanceof AppError9) {
|
|
4854
4863
|
throw error2;
|
|
4855
4864
|
} else {
|
|
4856
4865
|
throw new Error("Failed to update permission group.");
|
|
@@ -4926,7 +4935,7 @@ function usePermissionGroupRepo() {
|
|
|
4926
4935
|
try {
|
|
4927
4936
|
_id = new ObjectId16(_id);
|
|
4928
4937
|
} catch (error) {
|
|
4929
|
-
throw new
|
|
4938
|
+
throw new BadRequestError24("Invalid ID.");
|
|
4930
4939
|
}
|
|
4931
4940
|
const cacheKey = makeCacheKey11(namespace_collection, { _id: String(_id) });
|
|
4932
4941
|
try {
|
|
@@ -4954,7 +4963,7 @@ function usePermissionGroupRepo() {
|
|
|
4954
4963
|
});
|
|
4955
4964
|
return result;
|
|
4956
4965
|
} catch (error) {
|
|
4957
|
-
if (error instanceof
|
|
4966
|
+
if (error instanceof AppError9) {
|
|
4958
4967
|
throw error;
|
|
4959
4968
|
} else {
|
|
4960
4969
|
throw new InternalServerError15("Failed to get permission group.");
|
|
@@ -4962,13 +4971,13 @@ function usePermissionGroupRepo() {
|
|
|
4962
4971
|
}
|
|
4963
4972
|
}
|
|
4964
4973
|
async function getByKey(key, app) {
|
|
4965
|
-
const validation =
|
|
4966
|
-
key:
|
|
4967
|
-
app:
|
|
4974
|
+
const validation = Joi14.object({
|
|
4975
|
+
key: Joi14.string().required(),
|
|
4976
|
+
app: Joi14.string().optional().allow(null, "")
|
|
4968
4977
|
});
|
|
4969
4978
|
const { error } = validation.validate({ key, app });
|
|
4970
4979
|
if (error) {
|
|
4971
|
-
throw new
|
|
4980
|
+
throw new BadRequestError24("Invalid key.");
|
|
4972
4981
|
}
|
|
4973
4982
|
const query = { key };
|
|
4974
4983
|
const cacheKeyOptions = { key, tag: "byKey" };
|
|
@@ -5000,7 +5009,7 @@ function usePermissionGroupRepo() {
|
|
|
5000
5009
|
});
|
|
5001
5010
|
return result;
|
|
5002
5011
|
} catch (error2) {
|
|
5003
|
-
if (error2 instanceof
|
|
5012
|
+
if (error2 instanceof AppError9) {
|
|
5004
5013
|
throw error2;
|
|
5005
5014
|
} else {
|
|
5006
5015
|
throw new InternalServerError15("Failed to get permission group.");
|
|
@@ -5011,7 +5020,7 @@ function usePermissionGroupRepo() {
|
|
|
5011
5020
|
try {
|
|
5012
5021
|
_id = new ObjectId16(_id);
|
|
5013
5022
|
} catch (error) {
|
|
5014
|
-
throw new
|
|
5023
|
+
throw new BadRequestError24("Invalid ID.");
|
|
5015
5024
|
}
|
|
5016
5025
|
try {
|
|
5017
5026
|
const res = await repo.collection.updateOne(
|
|
@@ -5025,7 +5034,7 @@ function usePermissionGroupRepo() {
|
|
|
5025
5034
|
level: "error",
|
|
5026
5035
|
message: error.message
|
|
5027
5036
|
});
|
|
5028
|
-
if (error instanceof
|
|
5037
|
+
if (error instanceof AppError9) {
|
|
5029
5038
|
throw error;
|
|
5030
5039
|
} else {
|
|
5031
5040
|
throw new InternalServerError15("Failed to delete permission group.");
|
|
@@ -5045,11 +5054,12 @@ function usePermissionGroupRepo() {
|
|
|
5045
5054
|
|
|
5046
5055
|
// src/resources/permission/permission.group.service.ts
|
|
5047
5056
|
import {
|
|
5048
|
-
AppError as
|
|
5049
|
-
BadRequestError as
|
|
5057
|
+
AppError as AppError10,
|
|
5058
|
+
BadRequestError as BadRequestError25,
|
|
5050
5059
|
InternalServerError as InternalServerError16,
|
|
5051
5060
|
logger as logger17,
|
|
5052
|
-
useAtlas as
|
|
5061
|
+
useAtlas as useAtlas11,
|
|
5062
|
+
useModule
|
|
5053
5063
|
} from "@eeplatform/nodejs-utils";
|
|
5054
5064
|
function usePermissionGroupService() {
|
|
5055
5065
|
const {
|
|
@@ -5066,7 +5076,7 @@ function usePermissionGroupService() {
|
|
|
5066
5076
|
add: addPermission
|
|
5067
5077
|
} = usePermissionRepo();
|
|
5068
5078
|
async function addDefaultModule() {
|
|
5069
|
-
const session =
|
|
5079
|
+
const session = useAtlas11.getClient()?.startSession();
|
|
5070
5080
|
if (!session) {
|
|
5071
5081
|
throw new Error("Failed to start database session.");
|
|
5072
5082
|
}
|
|
@@ -5077,96 +5087,69 @@ function usePermissionGroupService() {
|
|
|
5077
5087
|
type: ["default", "standard"]
|
|
5078
5088
|
});
|
|
5079
5089
|
if (apps && apps.items && apps.items.length) {
|
|
5080
|
-
|
|
5081
|
-
|
|
5082
|
-
|
|
5083
|
-
|
|
5084
|
-
|
|
5085
|
-
|
|
5086
|
-
|
|
5087
|
-
|
|
5088
|
-
|
|
5089
|
-
|
|
5090
|
-
|
|
5091
|
-
|
|
5092
|
-
|
|
5093
|
-
|
|
5094
|
-
|
|
5095
|
-
|
|
5096
|
-
|
|
5097
|
-
|
|
5098
|
-
|
|
5099
|
-
|
|
5100
|
-
|
|
5101
|
-
|
|
5102
|
-
|
|
5103
|
-
|
|
5104
|
-
|
|
5105
|
-
|
|
5106
|
-
|
|
5107
|
-
|
|
5108
|
-
|
|
5109
|
-
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5116
|
-
|
|
5117
|
-
|
|
5118
|
-
|
|
5119
|
-
|
|
5120
|
-
|
|
5121
|
-
|
|
5122
|
-
|
|
5123
|
-
|
|
5124
|
-
|
|
5125
|
-
|
|
5126
|
-
|
|
5127
|
-
|
|
5128
|
-
|
|
5129
|
-
|
|
5130
|
-
|
|
5131
|
-
|
|
5132
|
-
|
|
5133
|
-
|
|
5134
|
-
|
|
5135
|
-
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
|
|
5139
|
-
|
|
5140
|
-
|
|
5141
|
-
|
|
5142
|
-
|
|
5143
|
-
{
|
|
5144
|
-
key: "invitations",
|
|
5145
|
-
label: "Invitations",
|
|
5146
|
-
permissions: [
|
|
5147
|
-
{
|
|
5148
|
-
key: "invitations.view",
|
|
5149
|
-
name: "View Invitations",
|
|
5150
|
-
description: "Allow user to view invitations"
|
|
5151
|
-
},
|
|
5152
|
-
{
|
|
5153
|
-
key: "invitations.view.details",
|
|
5154
|
-
name: "View Invitation Details",
|
|
5155
|
-
description: "Allow user to view invitation details"
|
|
5156
|
-
},
|
|
5157
|
-
{
|
|
5158
|
-
key: "invitations.send",
|
|
5159
|
-
name: "Send Invitations",
|
|
5160
|
-
description: "Allow user to send invitations"
|
|
5161
|
-
},
|
|
5162
|
-
{
|
|
5163
|
-
key: "invitations.revoke",
|
|
5164
|
-
name: "Revoke Invitations",
|
|
5165
|
-
description: "Allow user to revoke invitations"
|
|
5166
|
-
}
|
|
5167
|
-
]
|
|
5168
|
-
}
|
|
5169
|
-
];
|
|
5090
|
+
let modules = [];
|
|
5091
|
+
const memberModule = useModule("members", "Members").addPermission({
|
|
5092
|
+
key: "add",
|
|
5093
|
+
name: "Add Members",
|
|
5094
|
+
description: "Allow user to add new members"
|
|
5095
|
+
}).addPermission({
|
|
5096
|
+
key: "view",
|
|
5097
|
+
name: "View Members",
|
|
5098
|
+
description: "Allow user to view members"
|
|
5099
|
+
}).addPermission({
|
|
5100
|
+
key: "view.details",
|
|
5101
|
+
name: "View Member Details",
|
|
5102
|
+
description: "Allow user to view member details"
|
|
5103
|
+
}).addPermission({
|
|
5104
|
+
key: "edit.details",
|
|
5105
|
+
name: "Edit Member Details",
|
|
5106
|
+
description: "Allow user to edit member details"
|
|
5107
|
+
}).addPermission({
|
|
5108
|
+
key: "delete",
|
|
5109
|
+
name: "Delete Members",
|
|
5110
|
+
description: "Allow user to delete members"
|
|
5111
|
+
});
|
|
5112
|
+
modules.push(memberModule);
|
|
5113
|
+
const roleModule = useModule("roles", "Roles").addPermission({
|
|
5114
|
+
key: "add",
|
|
5115
|
+
name: "Add Roles",
|
|
5116
|
+
description: "Allow user to add new roles"
|
|
5117
|
+
}).addPermission({
|
|
5118
|
+
key: "view",
|
|
5119
|
+
name: "View Roles",
|
|
5120
|
+
description: "Allow user to view roles"
|
|
5121
|
+
}).addPermission({
|
|
5122
|
+
key: "view.details",
|
|
5123
|
+
name: "View Role Details",
|
|
5124
|
+
description: "Allow user to view role details"
|
|
5125
|
+
}).addPermission({
|
|
5126
|
+
key: "edit.details",
|
|
5127
|
+
name: "Edit Role Details",
|
|
5128
|
+
description: "Allow user to edit role details"
|
|
5129
|
+
}).addPermission({
|
|
5130
|
+
key: "delete",
|
|
5131
|
+
name: "Delete Roles",
|
|
5132
|
+
description: "Allow user to delete roles"
|
|
5133
|
+
});
|
|
5134
|
+
modules.push(roleModule);
|
|
5135
|
+
const invitationModule = useModule("invitations", "Invitations").addPermission({
|
|
5136
|
+
key: "send",
|
|
5137
|
+
name: "Send Invitations",
|
|
5138
|
+
description: "Allow user to send invitations"
|
|
5139
|
+
}).addPermission({
|
|
5140
|
+
key: "view",
|
|
5141
|
+
name: "View Invitations",
|
|
5142
|
+
description: "Allow user to view invitations"
|
|
5143
|
+
}).addPermission({
|
|
5144
|
+
key: "view.details",
|
|
5145
|
+
name: "View Invitation Details",
|
|
5146
|
+
description: "Allow user to view invitation details"
|
|
5147
|
+
}).addPermission({
|
|
5148
|
+
key: "revoke",
|
|
5149
|
+
name: "Revoke Invitations",
|
|
5150
|
+
description: "Allow user to revoke invitations"
|
|
5151
|
+
});
|
|
5152
|
+
modules.push(invitationModule);
|
|
5170
5153
|
for (const app of apps.items) {
|
|
5171
5154
|
for (const module of modules) {
|
|
5172
5155
|
const existingGroup = await _getByKey(module.key, app.code);
|
|
@@ -5199,168 +5182,426 @@ function usePermissionGroupService() {
|
|
|
5199
5182
|
}
|
|
5200
5183
|
}
|
|
5201
5184
|
if (app.code === "admin") {
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
|
|
5213
|
-
|
|
5214
|
-
|
|
5215
|
-
|
|
5216
|
-
|
|
5217
|
-
|
|
5218
|
-
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
5233
|
-
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
|
|
5241
|
-
|
|
5242
|
-
|
|
5243
|
-
|
|
5244
|
-
|
|
5245
|
-
|
|
5246
|
-
|
|
5247
|
-
|
|
5248
|
-
|
|
5249
|
-
|
|
5250
|
-
|
|
5251
|
-
|
|
5252
|
-
|
|
5253
|
-
|
|
5254
|
-
|
|
5255
|
-
|
|
5256
|
-
|
|
5257
|
-
|
|
5258
|
-
|
|
5259
|
-
|
|
5260
|
-
|
|
5261
|
-
|
|
5262
|
-
|
|
5263
|
-
|
|
5264
|
-
|
|
5265
|
-
|
|
5266
|
-
|
|
5267
|
-
|
|
5268
|
-
|
|
5269
|
-
|
|
5270
|
-
|
|
5271
|
-
|
|
5272
|
-
|
|
5273
|
-
|
|
5274
|
-
|
|
5275
|
-
|
|
5276
|
-
|
|
5277
|
-
|
|
5278
|
-
|
|
5279
|
-
|
|
5280
|
-
|
|
5281
|
-
|
|
5282
|
-
|
|
5283
|
-
|
|
5284
|
-
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5288
|
-
|
|
5289
|
-
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5294
|
-
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5298
|
-
|
|
5299
|
-
|
|
5300
|
-
|
|
5301
|
-
|
|
5302
|
-
|
|
5303
|
-
|
|
5304
|
-
|
|
5305
|
-
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5326
|
-
|
|
5327
|
-
|
|
5328
|
-
|
|
5329
|
-
|
|
5330
|
-
|
|
5331
|
-
|
|
5332
|
-
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
permissions: [
|
|
5336
|
-
{
|
|
5337
|
-
key: "users.add",
|
|
5338
|
-
name: "Add Users",
|
|
5339
|
-
description: "Allow user to add new users"
|
|
5340
|
-
},
|
|
5341
|
-
{
|
|
5342
|
-
key: "users.view",
|
|
5343
|
-
name: "View Users",
|
|
5344
|
-
description: "Allow user to view users"
|
|
5345
|
-
},
|
|
5346
|
-
{
|
|
5347
|
-
key: "users.view.details",
|
|
5348
|
-
name: "View User Details",
|
|
5349
|
-
description: "Allow user to view user details"
|
|
5350
|
-
},
|
|
5351
|
-
{
|
|
5352
|
-
key: "users.edit.status",
|
|
5353
|
-
name: "Edit User Status",
|
|
5354
|
-
description: "Allow user to edit user status"
|
|
5355
|
-
},
|
|
5356
|
-
{
|
|
5357
|
-
key: "users.delete",
|
|
5358
|
-
name: "Delete Users",
|
|
5359
|
-
description: "Allow user to delete users"
|
|
5360
|
-
}
|
|
5361
|
-
]
|
|
5185
|
+
let modules2 = [];
|
|
5186
|
+
const appModule = useModule("app", "Application").addPermission({
|
|
5187
|
+
key: "add",
|
|
5188
|
+
name: "Add Applications",
|
|
5189
|
+
description: "Allow user to add new applications"
|
|
5190
|
+
}).addPermission({
|
|
5191
|
+
key: "view",
|
|
5192
|
+
name: "View Applications",
|
|
5193
|
+
description: "Allow user to view applications"
|
|
5194
|
+
}).addPermission({
|
|
5195
|
+
key: "view.details",
|
|
5196
|
+
name: "View Application Details",
|
|
5197
|
+
description: "Allow user to view application details"
|
|
5198
|
+
}).addPermission({
|
|
5199
|
+
key: "edit",
|
|
5200
|
+
name: "Edit Applications",
|
|
5201
|
+
description: "Allow user to edit existing applications"
|
|
5202
|
+
}).addPermission({
|
|
5203
|
+
key: "update.details",
|
|
5204
|
+
name: "Update Application Details",
|
|
5205
|
+
description: "Allow user to update application details"
|
|
5206
|
+
}).addPermission({
|
|
5207
|
+
key: "delete",
|
|
5208
|
+
name: "Delete Applications",
|
|
5209
|
+
description: "Allow user to delete applications"
|
|
5210
|
+
});
|
|
5211
|
+
modules2.push(appModule);
|
|
5212
|
+
const userModule = useModule("users", "Users").addPermission({
|
|
5213
|
+
key: "add",
|
|
5214
|
+
name: "Add Users",
|
|
5215
|
+
description: "Allow user to add new users"
|
|
5216
|
+
}).addPermission({
|
|
5217
|
+
key: "view",
|
|
5218
|
+
name: "View Users",
|
|
5219
|
+
description: "Allow user to view users"
|
|
5220
|
+
}).addPermission({
|
|
5221
|
+
key: "view.details",
|
|
5222
|
+
name: "View User Details",
|
|
5223
|
+
description: "Allow user to view user details"
|
|
5224
|
+
}).addPermission({
|
|
5225
|
+
key: "edit.status",
|
|
5226
|
+
name: "Edit User Status",
|
|
5227
|
+
description: "Allow user to edit user status"
|
|
5228
|
+
}).addPermission({
|
|
5229
|
+
key: "delete",
|
|
5230
|
+
name: "Delete Users",
|
|
5231
|
+
description: "Allow user to delete users"
|
|
5232
|
+
});
|
|
5233
|
+
modules2.push(userModule);
|
|
5234
|
+
const basicEduRegionModule = useModule(
|
|
5235
|
+
"deped-ro",
|
|
5236
|
+
"Regional Offices"
|
|
5237
|
+
).addPermission({
|
|
5238
|
+
key: "add",
|
|
5239
|
+
name: "Add Regional Office",
|
|
5240
|
+
description: "Allow user to add new regional office"
|
|
5241
|
+
}).addPermission({
|
|
5242
|
+
key: "view",
|
|
5243
|
+
name: "View Regional Offices",
|
|
5244
|
+
description: "Allow user to view regional offices"
|
|
5245
|
+
}).addPermission({
|
|
5246
|
+
key: "view.details",
|
|
5247
|
+
name: "View Regional Office Details",
|
|
5248
|
+
description: "Allow user to view regional office details"
|
|
5249
|
+
}).addPermission({
|
|
5250
|
+
key: "update.details",
|
|
5251
|
+
name: "Update Regional Office Details",
|
|
5252
|
+
description: "Allow user to update regional office details"
|
|
5253
|
+
}).addPermission({
|
|
5254
|
+
key: "delete",
|
|
5255
|
+
name: "Delete Regional Office",
|
|
5256
|
+
description: "Allow user to delete regional office"
|
|
5257
|
+
});
|
|
5258
|
+
modules2.push(basicEduRegionModule);
|
|
5259
|
+
const basicEduDivisionModule = useModule(
|
|
5260
|
+
"deped-sdo",
|
|
5261
|
+
"School Division Offices"
|
|
5262
|
+
).addPermission({
|
|
5263
|
+
key: "add",
|
|
5264
|
+
name: "Add School Division Office",
|
|
5265
|
+
description: "Allow user to add new school division office"
|
|
5266
|
+
}).addPermission({
|
|
5267
|
+
key: "view",
|
|
5268
|
+
name: "View School Division Offices",
|
|
5269
|
+
description: "Allow user to view school division offices"
|
|
5270
|
+
}).addPermission({
|
|
5271
|
+
key: "view.details",
|
|
5272
|
+
name: "View School Division Office Details",
|
|
5273
|
+
description: "Allow user to view school division office details"
|
|
5274
|
+
}).addPermission({
|
|
5275
|
+
key: "update.details",
|
|
5276
|
+
name: "Update School Division Office Details",
|
|
5277
|
+
description: "Allow user to update school division office details"
|
|
5278
|
+
}).addPermission({
|
|
5279
|
+
key: "delete",
|
|
5280
|
+
name: "Delete School Division Office",
|
|
5281
|
+
description: "Allow user to delete school division office"
|
|
5282
|
+
});
|
|
5283
|
+
modules2.push(basicEduDivisionModule);
|
|
5284
|
+
const basicEduSchoolModule = useModule("deped-school", "Schools").addPermission({
|
|
5285
|
+
key: "add",
|
|
5286
|
+
name: "Add School",
|
|
5287
|
+
description: "Allow user to add new school"
|
|
5288
|
+
}).addPermission({
|
|
5289
|
+
key: "view",
|
|
5290
|
+
name: "View Schools",
|
|
5291
|
+
description: "Allow user to view schools"
|
|
5292
|
+
}).addPermission({
|
|
5293
|
+
key: "view.details",
|
|
5294
|
+
name: "View School Details",
|
|
5295
|
+
description: "Allow user to view school details"
|
|
5296
|
+
}).addPermission({
|
|
5297
|
+
key: "update.details",
|
|
5298
|
+
name: "Update School Details",
|
|
5299
|
+
description: "Allow user to update school details"
|
|
5300
|
+
}).addPermission({
|
|
5301
|
+
key: "delete",
|
|
5302
|
+
name: "Delete School",
|
|
5303
|
+
description: "Allow user to delete school"
|
|
5304
|
+
});
|
|
5305
|
+
modules2.push(basicEduSchoolModule);
|
|
5306
|
+
for (const module of modules2) {
|
|
5307
|
+
const existingGroup = await _getByKey(module.key, app.code);
|
|
5308
|
+
if (!existingGroup) {
|
|
5309
|
+
await _add({
|
|
5310
|
+
app: app.code,
|
|
5311
|
+
key: module.key,
|
|
5312
|
+
label: module.label
|
|
5313
|
+
});
|
|
5314
|
+
logger17.log({
|
|
5315
|
+
level: "info",
|
|
5316
|
+
message: `Default permission group added: ${app.code} - ${module.key}`
|
|
5317
|
+
});
|
|
5362
5318
|
}
|
|
5363
|
-
|
|
5319
|
+
for (const permission of module.permissions) {
|
|
5320
|
+
const existingPermission = await getPermissionByKey(
|
|
5321
|
+
permission.key,
|
|
5322
|
+
module.key
|
|
5323
|
+
);
|
|
5324
|
+
if (!existingPermission) {
|
|
5325
|
+
await addPermission({
|
|
5326
|
+
app: app.code,
|
|
5327
|
+
group: module.key,
|
|
5328
|
+
key: permission.key,
|
|
5329
|
+
name: permission.name,
|
|
5330
|
+
description: permission.description
|
|
5331
|
+
});
|
|
5332
|
+
}
|
|
5333
|
+
}
|
|
5334
|
+
}
|
|
5335
|
+
}
|
|
5336
|
+
if (app.code === "basic-edu-school") {
|
|
5337
|
+
const modules2 = [];
|
|
5338
|
+
const curriculumModule = useModule("curriculums", "Curriculums").addPermission({
|
|
5339
|
+
key: "add",
|
|
5340
|
+
name: "Add Curriculums",
|
|
5341
|
+
description: "Allow user to add new curriculums"
|
|
5342
|
+
}).addPermission({
|
|
5343
|
+
key: "view",
|
|
5344
|
+
name: "View Curriculums",
|
|
5345
|
+
description: "Allow user to view curriculums"
|
|
5346
|
+
}).addPermission({
|
|
5347
|
+
key: "view.details",
|
|
5348
|
+
name: "View Curriculum Details",
|
|
5349
|
+
description: "Allow user to view curriculum details"
|
|
5350
|
+
}).addPermission({
|
|
5351
|
+
key: "edit",
|
|
5352
|
+
name: "Edit Curriculums",
|
|
5353
|
+
description: "Allow user to edit existing curriculums"
|
|
5354
|
+
}).addPermission({
|
|
5355
|
+
key: "update.details",
|
|
5356
|
+
name: "Update Curriculum Details",
|
|
5357
|
+
description: "Allow user to update curriculum details"
|
|
5358
|
+
}).addPermission({
|
|
5359
|
+
key: "delete",
|
|
5360
|
+
name: "Delete Curriculums",
|
|
5361
|
+
description: "Allow user to delete curriculums"
|
|
5362
|
+
});
|
|
5363
|
+
modules2.push(curriculumModule);
|
|
5364
|
+
const gradeLevelModule = useModule("grade-levels", "Grade Levels").addPermission({
|
|
5365
|
+
key: "add",
|
|
5366
|
+
name: "Add Grade Levels",
|
|
5367
|
+
description: "Allow user to add new grade levels"
|
|
5368
|
+
}).addPermission({
|
|
5369
|
+
key: "view",
|
|
5370
|
+
name: "View Grade Levels",
|
|
5371
|
+
description: "Allow user to view grade levels"
|
|
5372
|
+
}).addPermission({
|
|
5373
|
+
key: "view.details",
|
|
5374
|
+
name: "View Grade Level Details",
|
|
5375
|
+
description: "Allow user to view grade level details"
|
|
5376
|
+
}).addPermission({
|
|
5377
|
+
key: "edit",
|
|
5378
|
+
name: "Edit Grade Levels",
|
|
5379
|
+
description: "Allow user to edit existing grade levels"
|
|
5380
|
+
}).addPermission({
|
|
5381
|
+
key: "update.details",
|
|
5382
|
+
name: "Update Grade Level Details",
|
|
5383
|
+
description: "Allow user to update grade level details"
|
|
5384
|
+
}).addPermission({
|
|
5385
|
+
key: "delete",
|
|
5386
|
+
name: "Delete Grade Levels",
|
|
5387
|
+
description: "Allow user to delete grade levels"
|
|
5388
|
+
});
|
|
5389
|
+
modules2.push(gradeLevelModule);
|
|
5390
|
+
const programModule = useModule("programs", "Programs").addPermission({
|
|
5391
|
+
key: "add",
|
|
5392
|
+
name: "Add Programs",
|
|
5393
|
+
description: "Allow user to add new programs"
|
|
5394
|
+
}).addPermission({
|
|
5395
|
+
key: "view",
|
|
5396
|
+
name: "View Programs",
|
|
5397
|
+
description: "Allow user to view programs"
|
|
5398
|
+
}).addPermission({
|
|
5399
|
+
key: "view.details",
|
|
5400
|
+
name: "View Program Details",
|
|
5401
|
+
description: "Allow user to view program details"
|
|
5402
|
+
}).addPermission({
|
|
5403
|
+
key: "edit",
|
|
5404
|
+
name: "Edit Programs",
|
|
5405
|
+
description: "Allow user to edit existing programs"
|
|
5406
|
+
}).addPermission({
|
|
5407
|
+
key: "update.details",
|
|
5408
|
+
name: "Update Program Details",
|
|
5409
|
+
description: "Allow user to update program details"
|
|
5410
|
+
}).addPermission({
|
|
5411
|
+
key: "delete",
|
|
5412
|
+
name: "Delete Programs",
|
|
5413
|
+
description: "Allow user to delete programs"
|
|
5414
|
+
});
|
|
5415
|
+
modules2.push(programModule);
|
|
5416
|
+
const kindergartenRoutineModule = useModule(
|
|
5417
|
+
"kindergarten-routines",
|
|
5418
|
+
"Kindergarten Routines"
|
|
5419
|
+
).addPermission({
|
|
5420
|
+
key: "add",
|
|
5421
|
+
name: "Add Kindergarten Routines",
|
|
5422
|
+
description: "Allow user to add new kindergarten routines"
|
|
5423
|
+
}).addPermission({
|
|
5424
|
+
key: "view",
|
|
5425
|
+
name: "View Kindergarten Routines",
|
|
5426
|
+
description: "Allow user to view kindergarten routines"
|
|
5427
|
+
}).addPermission({
|
|
5428
|
+
key: "view.details",
|
|
5429
|
+
name: "View Kindergarten Routine Details",
|
|
5430
|
+
description: "Allow user to view kindergarten routine details"
|
|
5431
|
+
}).addPermission({
|
|
5432
|
+
key: "edit",
|
|
5433
|
+
name: "Edit Kindergarten Routines",
|
|
5434
|
+
description: "Allow user to edit existing kindergarten routines"
|
|
5435
|
+
}).addPermission({
|
|
5436
|
+
key: "update.details",
|
|
5437
|
+
name: "Update Kindergarten Routine Details",
|
|
5438
|
+
description: "Allow user to update kindergarten routine details"
|
|
5439
|
+
}).addPermission({
|
|
5440
|
+
key: "delete",
|
|
5441
|
+
name: "Delete Kindergarten Routines",
|
|
5442
|
+
description: "Allow user to delete kindergarten routines"
|
|
5443
|
+
});
|
|
5444
|
+
modules2.push(kindergartenRoutineModule);
|
|
5445
|
+
const subjectModule = useModule("subjects", "Subjects").addPermission({
|
|
5446
|
+
key: "add",
|
|
5447
|
+
name: "Add Subjects",
|
|
5448
|
+
description: "Allow user to add new subjects"
|
|
5449
|
+
}).addPermission({
|
|
5450
|
+
key: "view",
|
|
5451
|
+
name: "View Subjects",
|
|
5452
|
+
description: "Allow user to view subjects"
|
|
5453
|
+
}).addPermission({
|
|
5454
|
+
key: "view.details",
|
|
5455
|
+
name: "View Subject Details",
|
|
5456
|
+
description: "Allow user to view subject details"
|
|
5457
|
+
}).addPermission({
|
|
5458
|
+
key: "edit",
|
|
5459
|
+
name: "Edit Subjects",
|
|
5460
|
+
description: "Allow user to edit existing subjects"
|
|
5461
|
+
}).addPermission({
|
|
5462
|
+
key: "update.details",
|
|
5463
|
+
name: "Update Subject Details",
|
|
5464
|
+
description: "Allow user to update subject details"
|
|
5465
|
+
}).addPermission({
|
|
5466
|
+
key: "delete",
|
|
5467
|
+
name: "Delete Subjects",
|
|
5468
|
+
description: "Allow user to delete subjects"
|
|
5469
|
+
});
|
|
5470
|
+
modules2.push(subjectModule);
|
|
5471
|
+
const teachingLoadModule = useModule(
|
|
5472
|
+
"teaching-loads",
|
|
5473
|
+
"Teaching Loads"
|
|
5474
|
+
).addPermission({
|
|
5475
|
+
key: "add",
|
|
5476
|
+
name: "Add Teaching Loads",
|
|
5477
|
+
description: "Allow user to add new teaching loads"
|
|
5478
|
+
}).addPermission({
|
|
5479
|
+
key: "view",
|
|
5480
|
+
name: "View Teaching Loads",
|
|
5481
|
+
description: "Allow user to view teaching loads"
|
|
5482
|
+
}).addPermission({
|
|
5483
|
+
key: "view.details",
|
|
5484
|
+
name: "View Teaching Load Details",
|
|
5485
|
+
description: "Allow user to view teaching load details"
|
|
5486
|
+
}).addPermission({
|
|
5487
|
+
key: "edit",
|
|
5488
|
+
name: "Edit Teaching Loads",
|
|
5489
|
+
description: "Allow user to edit existing teaching loads"
|
|
5490
|
+
}).addPermission({
|
|
5491
|
+
key: "update.details",
|
|
5492
|
+
name: "Update Teaching Load Details",
|
|
5493
|
+
description: "Allow user to update teaching load details"
|
|
5494
|
+
}).addPermission({
|
|
5495
|
+
key: "delete",
|
|
5496
|
+
name: "Delete Teaching Loads",
|
|
5497
|
+
description: "Allow user to delete teaching loads"
|
|
5498
|
+
});
|
|
5499
|
+
modules2.push(teachingLoadModule);
|
|
5500
|
+
const personnelModule = useModule("personnel", "Personnel").addPermission({
|
|
5501
|
+
key: "add",
|
|
5502
|
+
name: "Add Personnel",
|
|
5503
|
+
description: "Allow user to add new personnel"
|
|
5504
|
+
}).addPermission({
|
|
5505
|
+
key: "view",
|
|
5506
|
+
name: "View Personnel",
|
|
5507
|
+
description: "Allow user to view personnel"
|
|
5508
|
+
}).addPermission({
|
|
5509
|
+
key: "view.details",
|
|
5510
|
+
name: "View Personnel Details",
|
|
5511
|
+
description: "Allow user to view personnel details"
|
|
5512
|
+
}).addPermission({
|
|
5513
|
+
key: "edit",
|
|
5514
|
+
name: "Edit Personnel",
|
|
5515
|
+
description: "Allow user to edit existing personnel"
|
|
5516
|
+
}).addPermission({
|
|
5517
|
+
key: "update.details",
|
|
5518
|
+
name: "Update Personnel Details",
|
|
5519
|
+
description: "Allow user to update personnel details"
|
|
5520
|
+
}).addPermission({
|
|
5521
|
+
key: "delete",
|
|
5522
|
+
name: "Delete Personnel",
|
|
5523
|
+
description: "Allow user to delete personnel"
|
|
5524
|
+
});
|
|
5525
|
+
modules2.push(personnelModule);
|
|
5526
|
+
const studentModule = useModule("students", "Students").addPermission({
|
|
5527
|
+
key: "view",
|
|
5528
|
+
name: "View Students",
|
|
5529
|
+
description: "Allow user to view students"
|
|
5530
|
+
}).addPermission({
|
|
5531
|
+
key: "view.details",
|
|
5532
|
+
name: "View Student Details",
|
|
5533
|
+
description: "Allow user to view student details"
|
|
5534
|
+
}).addPermission({
|
|
5535
|
+
key: "update.details",
|
|
5536
|
+
name: "Update Student Details",
|
|
5537
|
+
description: "Allow user to update student details"
|
|
5538
|
+
}).addPermission({
|
|
5539
|
+
key: "update.transfer",
|
|
5540
|
+
name: "Update Student Transfer",
|
|
5541
|
+
description: "Allow user to update student transfer details"
|
|
5542
|
+
});
|
|
5543
|
+
modules2.push(studentModule);
|
|
5544
|
+
const sectionModule = useModule("sections", "Sections").addPermission({
|
|
5545
|
+
key: "generate",
|
|
5546
|
+
name: "Generate Sections",
|
|
5547
|
+
description: "Allow user to generate sections"
|
|
5548
|
+
}).addPermission({
|
|
5549
|
+
key: "view",
|
|
5550
|
+
name: "View Sections",
|
|
5551
|
+
description: "Allow user to view sections"
|
|
5552
|
+
}).addPermission({
|
|
5553
|
+
key: "view.details",
|
|
5554
|
+
name: "View Section Details",
|
|
5555
|
+
description: "Allow user to view section details"
|
|
5556
|
+
}).addPermission({
|
|
5557
|
+
key: "update.details",
|
|
5558
|
+
name: "Update Section Details",
|
|
5559
|
+
description: "Allow user to update section details"
|
|
5560
|
+
}).addPermission({
|
|
5561
|
+
key: "delete",
|
|
5562
|
+
name: "Delete Sections",
|
|
5563
|
+
description: "Allow user to delete sections"
|
|
5564
|
+
});
|
|
5565
|
+
modules2.push(sectionModule);
|
|
5566
|
+
const enrollmentModule = useModule("enrollments", "Enrollments").addPermission({
|
|
5567
|
+
key: "add",
|
|
5568
|
+
name: "Add Enrollments",
|
|
5569
|
+
description: "Allow user to add new enrollments"
|
|
5570
|
+
}).addPermission({
|
|
5571
|
+
key: "view",
|
|
5572
|
+
name: "View Enrollments",
|
|
5573
|
+
description: "Allow user to view enrollments"
|
|
5574
|
+
}).addPermission({
|
|
5575
|
+
key: "view.details",
|
|
5576
|
+
name: "View Enrollment Details",
|
|
5577
|
+
description: "Allow user to view enrollment details"
|
|
5578
|
+
}).addPermission({
|
|
5579
|
+
key: "accept",
|
|
5580
|
+
name: "Accept Enrollments",
|
|
5581
|
+
description: "Allow user to accept enrollments"
|
|
5582
|
+
}).addPermission({
|
|
5583
|
+
key: "reject",
|
|
5584
|
+
name: "Reject Enrollments",
|
|
5585
|
+
description: "Allow user to reject enrollments"
|
|
5586
|
+
});
|
|
5587
|
+
modules2.push(enrollmentModule);
|
|
5588
|
+
const programScreeningModule = useModule(
|
|
5589
|
+
"program-screening",
|
|
5590
|
+
"Program Screening"
|
|
5591
|
+
).addPermission({
|
|
5592
|
+
key: "view",
|
|
5593
|
+
name: "View Program Screenings",
|
|
5594
|
+
description: "Allow user to view program screenings"
|
|
5595
|
+
}).addPermission({
|
|
5596
|
+
key: "approve",
|
|
5597
|
+
name: "Approve Program Screening",
|
|
5598
|
+
description: "Allow user to approve program screening"
|
|
5599
|
+
}).addPermission({
|
|
5600
|
+
key: "reject",
|
|
5601
|
+
name: "Reject Program Screening",
|
|
5602
|
+
description: "Allow user to reject program screening"
|
|
5603
|
+
});
|
|
5604
|
+
modules2.push(programScreeningModule);
|
|
5364
5605
|
for (const module of modules2) {
|
|
5365
5606
|
const existingGroup = await _getByKey(module.key, app.code);
|
|
5366
5607
|
if (!existingGroup) {
|
|
@@ -5414,15 +5655,15 @@ function usePermissionGroupService() {
|
|
|
5414
5655
|
}
|
|
5415
5656
|
const associatedPermissionsCount = await countByGroup(permission.key);
|
|
5416
5657
|
if (associatedPermissionsCount > 0) {
|
|
5417
|
-
throw new
|
|
5658
|
+
throw new BadRequestError25(
|
|
5418
5659
|
"Cannot delete Permission Group with associated Permissions."
|
|
5419
5660
|
);
|
|
5420
5661
|
}
|
|
5421
5662
|
try {
|
|
5422
5663
|
await _deleteById(id);
|
|
5423
|
-
return "Permission deleted successfully.";
|
|
5664
|
+
return "Permission Group deleted successfully.";
|
|
5424
5665
|
} catch (error) {
|
|
5425
|
-
if (error instanceof
|
|
5666
|
+
if (error instanceof AppError10) {
|
|
5426
5667
|
throw error;
|
|
5427
5668
|
} else {
|
|
5428
5669
|
throw new InternalServerError16("Failed to delete Permission Group.");
|
|
@@ -5436,8 +5677,8 @@ function usePermissionGroupService() {
|
|
|
5436
5677
|
}
|
|
5437
5678
|
|
|
5438
5679
|
// src/resources/permission/permission.group.controller.ts
|
|
5439
|
-
import { BadRequestError as
|
|
5440
|
-
import
|
|
5680
|
+
import { BadRequestError as BadRequestError26, logger as logger18 } from "@eeplatform/nodejs-utils";
|
|
5681
|
+
import Joi15 from "joi";
|
|
5441
5682
|
function usePermissionGroupController() {
|
|
5442
5683
|
const {
|
|
5443
5684
|
getAll: _getAll,
|
|
@@ -5450,7 +5691,7 @@ function usePermissionGroupController() {
|
|
|
5450
5691
|
const value = req.body;
|
|
5451
5692
|
const { error } = schemaPermissionGroup.validate(value);
|
|
5452
5693
|
if (error) {
|
|
5453
|
-
next(new
|
|
5694
|
+
next(new BadRequestError26(error.message));
|
|
5454
5695
|
logger18.info(`Controller: ${error.message}`);
|
|
5455
5696
|
return;
|
|
5456
5697
|
}
|
|
@@ -5464,16 +5705,16 @@ function usePermissionGroupController() {
|
|
|
5464
5705
|
}
|
|
5465
5706
|
async function getAll(req, res, next) {
|
|
5466
5707
|
const query = req.query;
|
|
5467
|
-
const validation =
|
|
5468
|
-
page:
|
|
5469
|
-
limit:
|
|
5470
|
-
search:
|
|
5471
|
-
app:
|
|
5472
|
-
status:
|
|
5708
|
+
const validation = Joi15.object({
|
|
5709
|
+
page: Joi15.number().min(1).optional().allow("", null),
|
|
5710
|
+
limit: Joi15.number().min(1).optional().allow("", null),
|
|
5711
|
+
search: Joi15.string().optional().allow("", null),
|
|
5712
|
+
app: Joi15.string().optional().allow("", null),
|
|
5713
|
+
status: Joi15.string().optional().allow("", null)
|
|
5473
5714
|
});
|
|
5474
5715
|
const { error } = validation.validate(query);
|
|
5475
5716
|
if (error) {
|
|
5476
|
-
next(new
|
|
5717
|
+
next(new BadRequestError26(error.message));
|
|
5477
5718
|
return;
|
|
5478
5719
|
}
|
|
5479
5720
|
const page = parseInt(req.query.page) ?? 1;
|
|
@@ -5507,12 +5748,12 @@ function usePermissionGroupController() {
|
|
|
5507
5748
|
}
|
|
5508
5749
|
async function getById(req, res, next) {
|
|
5509
5750
|
const id = req.params.id;
|
|
5510
|
-
const validation =
|
|
5511
|
-
id:
|
|
5751
|
+
const validation = Joi15.object({
|
|
5752
|
+
id: Joi15.string().hex().required()
|
|
5512
5753
|
});
|
|
5513
5754
|
const { error } = validation.validate({ id });
|
|
5514
5755
|
if (error) {
|
|
5515
|
-
next(new
|
|
5756
|
+
next(new BadRequestError26(error.message));
|
|
5516
5757
|
return;
|
|
5517
5758
|
}
|
|
5518
5759
|
try {
|
|
@@ -5528,12 +5769,12 @@ function usePermissionGroupController() {
|
|
|
5528
5769
|
}
|
|
5529
5770
|
async function deleteById(req, res, next) {
|
|
5530
5771
|
const id = req.params.id;
|
|
5531
|
-
const validation =
|
|
5532
|
-
id:
|
|
5772
|
+
const validation = Joi15.object({
|
|
5773
|
+
id: Joi15.string().hex().required()
|
|
5533
5774
|
});
|
|
5534
5775
|
const { error } = validation.validate({ id });
|
|
5535
5776
|
if (error) {
|
|
5536
|
-
next(new
|
|
5777
|
+
next(new BadRequestError26(error.message));
|
|
5537
5778
|
return;
|
|
5538
5779
|
}
|
|
5539
5780
|
try {
|
|
@@ -5546,15 +5787,15 @@ function usePermissionGroupController() {
|
|
|
5546
5787
|
}
|
|
5547
5788
|
async function updateById(req, res, next) {
|
|
5548
5789
|
const id = req.params.id;
|
|
5549
|
-
const { error: errorId } =
|
|
5790
|
+
const { error: errorId } = Joi15.string().hex().required().validate(id);
|
|
5550
5791
|
if (errorId) {
|
|
5551
|
-
next(new
|
|
5792
|
+
next(new BadRequestError26(errorId.message));
|
|
5552
5793
|
return;
|
|
5553
5794
|
}
|
|
5554
5795
|
const payload = req.body;
|
|
5555
5796
|
const { error } = schemaPermissionGroupUpdate.validate(payload);
|
|
5556
5797
|
if (error) {
|
|
5557
|
-
next(new
|
|
5798
|
+
next(new BadRequestError26(error.message));
|
|
5558
5799
|
return;
|
|
5559
5800
|
}
|
|
5560
5801
|
try {
|
|
@@ -5575,7 +5816,7 @@ function usePermissionGroupController() {
|
|
|
5575
5816
|
}
|
|
5576
5817
|
|
|
5577
5818
|
// src/resources/file/file.service.ts
|
|
5578
|
-
import { logger as logger19, useS3 as useS32, useAtlas as
|
|
5819
|
+
import { logger as logger19, useS3 as useS32, useAtlas as useAtlas12 } from "@eeplatform/nodejs-utils";
|
|
5579
5820
|
import cron from "node-cron";
|
|
5580
5821
|
import * as fs from "fs";
|
|
5581
5822
|
function useFileService() {
|
|
@@ -5593,7 +5834,7 @@ function useFileService() {
|
|
|
5593
5834
|
forcePathStyle: true
|
|
5594
5835
|
});
|
|
5595
5836
|
async function createFile(value) {
|
|
5596
|
-
const session =
|
|
5837
|
+
const session = useAtlas12.getClient()?.startSession();
|
|
5597
5838
|
session?.startTransaction();
|
|
5598
5839
|
const file = {
|
|
5599
5840
|
name: value.originalname,
|
|
@@ -5627,7 +5868,7 @@ function useFileService() {
|
|
|
5627
5868
|
}
|
|
5628
5869
|
}
|
|
5629
5870
|
async function deleteFile(id) {
|
|
5630
|
-
const session =
|
|
5871
|
+
const session = useAtlas12.getClient()?.startSession();
|
|
5631
5872
|
session?.startTransaction();
|
|
5632
5873
|
try {
|
|
5633
5874
|
await deleteFileById(id, session);
|
|
@@ -5671,11 +5912,11 @@ function useFileService() {
|
|
|
5671
5912
|
|
|
5672
5913
|
// src/resources/file/file.controller.ts
|
|
5673
5914
|
import {
|
|
5674
|
-
AppError as
|
|
5675
|
-
BadRequestError as
|
|
5915
|
+
AppError as AppError11,
|
|
5916
|
+
BadRequestError as BadRequestError27,
|
|
5676
5917
|
InternalServerError as InternalServerError17
|
|
5677
5918
|
} from "@eeplatform/nodejs-utils";
|
|
5678
|
-
import
|
|
5919
|
+
import Joi16 from "joi";
|
|
5679
5920
|
function useFileController() {
|
|
5680
5921
|
const { createFile, deleteFile: _deleteFile } = useFileService();
|
|
5681
5922
|
async function upload(req, res, next) {
|
|
@@ -5688,7 +5929,7 @@ function useFileController() {
|
|
|
5688
5929
|
res.json({ message: "Successfully uploaded file", id });
|
|
5689
5930
|
return;
|
|
5690
5931
|
} catch (error) {
|
|
5691
|
-
if (error instanceof
|
|
5932
|
+
if (error instanceof AppError11) {
|
|
5692
5933
|
next(error);
|
|
5693
5934
|
} else {
|
|
5694
5935
|
next(new InternalServerError17(error));
|
|
@@ -5697,17 +5938,17 @@ function useFileController() {
|
|
|
5697
5938
|
}
|
|
5698
5939
|
async function deleteFile(req, res, next) {
|
|
5699
5940
|
const id = req.params.id;
|
|
5700
|
-
const validation =
|
|
5941
|
+
const validation = Joi16.string().required();
|
|
5701
5942
|
const { error } = validation.validate(id);
|
|
5702
5943
|
if (error) {
|
|
5703
|
-
next(new
|
|
5944
|
+
next(new BadRequestError27(error.message));
|
|
5704
5945
|
}
|
|
5705
5946
|
try {
|
|
5706
5947
|
const message = await _deleteFile(id);
|
|
5707
5948
|
res.json({ message });
|
|
5708
5949
|
return;
|
|
5709
5950
|
} catch (error2) {
|
|
5710
|
-
if (error2 instanceof
|
|
5951
|
+
if (error2 instanceof AppError11) {
|
|
5711
5952
|
next(error2);
|
|
5712
5953
|
} else {
|
|
5713
5954
|
next(new InternalServerError17(error2));
|
|
@@ -5720,21 +5961,16 @@ function useFileController() {
|
|
|
5720
5961
|
};
|
|
5721
5962
|
}
|
|
5722
5963
|
|
|
5723
|
-
// src/resources/role/role.controller.ts
|
|
5724
|
-
import Joi18 from "joi";
|
|
5725
|
-
import { BadRequestError as BadRequestError29 } from "@eeplatform/nodejs-utils";
|
|
5726
|
-
|
|
5727
5964
|
// src/resources/role/role.service.ts
|
|
5728
5965
|
import {
|
|
5729
|
-
AppError as
|
|
5730
|
-
BadRequestError as
|
|
5966
|
+
AppError as AppError12,
|
|
5967
|
+
BadRequestError as BadRequestError29,
|
|
5731
5968
|
InternalServerError as InternalServerError18
|
|
5732
5969
|
} from "@eeplatform/nodejs-utils";
|
|
5733
|
-
import Joi17 from "joi";
|
|
5734
5970
|
|
|
5735
5971
|
// src/resources/member/member.controller.ts
|
|
5736
|
-
import
|
|
5737
|
-
import { BadRequestError as
|
|
5972
|
+
import Joi17 from "joi";
|
|
5973
|
+
import { BadRequestError as BadRequestError28 } from "@eeplatform/nodejs-utils";
|
|
5738
5974
|
function useMemberController() {
|
|
5739
5975
|
const {
|
|
5740
5976
|
getByUserId: _getByUserId,
|
|
@@ -5745,12 +5981,12 @@ function useMemberController() {
|
|
|
5745
5981
|
} = useMemberRepo();
|
|
5746
5982
|
async function getByUserId(req, res, next) {
|
|
5747
5983
|
const userId = req.params.id;
|
|
5748
|
-
const validation =
|
|
5749
|
-
id:
|
|
5984
|
+
const validation = Joi17.object({
|
|
5985
|
+
id: Joi17.string().hex().required()
|
|
5750
5986
|
});
|
|
5751
5987
|
const { error } = validation.validate({ id: userId });
|
|
5752
5988
|
if (error) {
|
|
5753
|
-
next(new
|
|
5989
|
+
next(new BadRequestError28(error.message));
|
|
5754
5990
|
return;
|
|
5755
5991
|
}
|
|
5756
5992
|
try {
|
|
@@ -5765,14 +6001,14 @@ function useMemberController() {
|
|
|
5765
6001
|
}
|
|
5766
6002
|
}
|
|
5767
6003
|
async function getByUserType(req, res, next) {
|
|
5768
|
-
const validation =
|
|
5769
|
-
org:
|
|
5770
|
-
user:
|
|
5771
|
-
type:
|
|
6004
|
+
const validation = Joi17.object({
|
|
6005
|
+
org: Joi17.string().hex().optional().allow("", null),
|
|
6006
|
+
user: Joi17.string().hex().required(),
|
|
6007
|
+
type: Joi17.string().required()
|
|
5772
6008
|
});
|
|
5773
6009
|
const { error } = validation.validate({ ...req.params, ...req.query });
|
|
5774
6010
|
if (error) {
|
|
5775
|
-
next(new
|
|
6011
|
+
next(new BadRequestError28(error.message));
|
|
5776
6012
|
return;
|
|
5777
6013
|
}
|
|
5778
6014
|
const orgId = req.query.org;
|
|
@@ -5797,14 +6033,14 @@ function useMemberController() {
|
|
|
5797
6033
|
const org = req.query.org ?? "";
|
|
5798
6034
|
const type = req.query.type ?? "main";
|
|
5799
6035
|
const status = req.query.status ?? "active";
|
|
5800
|
-
const validation =
|
|
5801
|
-
limit:
|
|
5802
|
-
search:
|
|
5803
|
-
page:
|
|
5804
|
-
user:
|
|
5805
|
-
org:
|
|
5806
|
-
type:
|
|
5807
|
-
status:
|
|
6036
|
+
const validation = Joi17.object({
|
|
6037
|
+
limit: Joi17.number().min(10).max(300).required(),
|
|
6038
|
+
search: Joi17.string().optional().allow("", null),
|
|
6039
|
+
page: Joi17.number().required(),
|
|
6040
|
+
user: Joi17.string().hex().optional().allow("", null),
|
|
6041
|
+
org: Joi17.string().hex().optional().allow("", null),
|
|
6042
|
+
type: Joi17.string().required(),
|
|
6043
|
+
status: Joi17.string().required()
|
|
5808
6044
|
});
|
|
5809
6045
|
const { error } = validation.validate({
|
|
5810
6046
|
search,
|
|
@@ -5816,7 +6052,7 @@ function useMemberController() {
|
|
|
5816
6052
|
status
|
|
5817
6053
|
});
|
|
5818
6054
|
if (error) {
|
|
5819
|
-
next(new
|
|
6055
|
+
next(new BadRequestError28(error.message));
|
|
5820
6056
|
return;
|
|
5821
6057
|
}
|
|
5822
6058
|
try {
|
|
@@ -5840,11 +6076,11 @@ function useMemberController() {
|
|
|
5840
6076
|
const search = req.query.search ?? "";
|
|
5841
6077
|
const page = Number(req.query.page) ?? 1;
|
|
5842
6078
|
const user = req.query.user ?? "";
|
|
5843
|
-
const validation =
|
|
5844
|
-
limit:
|
|
5845
|
-
search:
|
|
5846
|
-
page:
|
|
5847
|
-
user:
|
|
6079
|
+
const validation = Joi17.object({
|
|
6080
|
+
limit: Joi17.number().min(10).max(50).required(),
|
|
6081
|
+
search: Joi17.string().optional().allow("", null),
|
|
6082
|
+
page: Joi17.number().required(),
|
|
6083
|
+
user: Joi17.string().hex().optional().allow("", null)
|
|
5848
6084
|
});
|
|
5849
6085
|
const { error } = validation.validate({
|
|
5850
6086
|
search,
|
|
@@ -5853,7 +6089,7 @@ function useMemberController() {
|
|
|
5853
6089
|
limit
|
|
5854
6090
|
});
|
|
5855
6091
|
if (error) {
|
|
5856
|
-
next(new
|
|
6092
|
+
next(new BadRequestError28(error.message));
|
|
5857
6093
|
}
|
|
5858
6094
|
try {
|
|
5859
6095
|
const items = await _getOrgsByMembership({
|
|
@@ -5869,13 +6105,13 @@ function useMemberController() {
|
|
|
5869
6105
|
}
|
|
5870
6106
|
}
|
|
5871
6107
|
async function updateStatusByUserId(req, res, next) {
|
|
5872
|
-
const validation =
|
|
5873
|
-
id:
|
|
5874
|
-
status:
|
|
6108
|
+
const validation = Joi17.object({
|
|
6109
|
+
id: Joi17.string().hex().required(),
|
|
6110
|
+
status: Joi17.string().valid("active", "suspended", "deleted").required()
|
|
5875
6111
|
});
|
|
5876
6112
|
const { error } = validation.validate(req.params);
|
|
5877
6113
|
if (error) {
|
|
5878
|
-
next(new
|
|
6114
|
+
next(new BadRequestError28(error.message));
|
|
5879
6115
|
return;
|
|
5880
6116
|
}
|
|
5881
6117
|
const id = req.params.id;
|
|
@@ -5898,29 +6134,21 @@ function useMemberController() {
|
|
|
5898
6134
|
|
|
5899
6135
|
// src/resources/role/role.service.ts
|
|
5900
6136
|
function useRoleService() {
|
|
5901
|
-
const {
|
|
5902
|
-
const {
|
|
5903
|
-
async function deleteById(
|
|
5904
|
-
const { error } = Joi17.string().hex().length(24).validate(_id);
|
|
5905
|
-
if (error) {
|
|
5906
|
-
throw new BadRequestError28("Invalid Role ID");
|
|
5907
|
-
}
|
|
6137
|
+
const { getByRole } = useMemberRepo();
|
|
6138
|
+
const { deleteById: _deleteById } = useRoleRepo();
|
|
6139
|
+
async function deleteById(id) {
|
|
5908
6140
|
try {
|
|
5909
|
-
const role = await
|
|
5910
|
-
if (
|
|
5911
|
-
throw new
|
|
6141
|
+
const role = await getByRole(id);
|
|
6142
|
+
if (role) {
|
|
6143
|
+
throw new BadRequestError29("Cannot delete role assigned to members.");
|
|
5912
6144
|
}
|
|
5913
|
-
|
|
5914
|
-
|
|
5915
|
-
|
|
5916
|
-
|
|
5917
|
-
|
|
5918
|
-
|
|
5919
|
-
} catch (error2) {
|
|
5920
|
-
if (error2 instanceof AppError11) {
|
|
5921
|
-
throw error2;
|
|
6145
|
+
await _deleteById(id);
|
|
6146
|
+
} catch (error) {
|
|
6147
|
+
if (error instanceof AppError12) {
|
|
6148
|
+
throw error;
|
|
6149
|
+
} else {
|
|
6150
|
+
throw new InternalServerError18("Failed to delete role.");
|
|
5922
6151
|
}
|
|
5923
|
-
throw new InternalServerError18("Failed to delete role");
|
|
5924
6152
|
}
|
|
5925
6153
|
}
|
|
5926
6154
|
return {
|
|
@@ -5929,26 +6157,23 @@ function useRoleService() {
|
|
|
5929
6157
|
}
|
|
5930
6158
|
|
|
5931
6159
|
// src/resources/role/role.controller.ts
|
|
6160
|
+
import Joi18 from "joi";
|
|
6161
|
+
import { BadRequestError as BadRequestError30 } from "@eeplatform/nodejs-utils";
|
|
5932
6162
|
function useRoleController() {
|
|
5933
6163
|
const {
|
|
5934
6164
|
addRole: _createRole,
|
|
5935
|
-
|
|
6165
|
+
getById,
|
|
5936
6166
|
getRoleByUserId: _getRoleByUserId,
|
|
5937
6167
|
getRoles: _getRoles,
|
|
5938
6168
|
updateRole: _updateRole,
|
|
5939
6169
|
updatePermissionsById: _updatePermissionsById
|
|
5940
6170
|
} = useRoleRepo();
|
|
6171
|
+
const { deleteById: _deleteById } = useRoleService();
|
|
5941
6172
|
async function createRole(req, res, next) {
|
|
5942
6173
|
const payload = req.body;
|
|
5943
|
-
const
|
|
5944
|
-
name: Joi18.string().required(),
|
|
5945
|
-
permissions: Joi18.array().items(Joi18.string()).required(),
|
|
5946
|
-
type: Joi18.string().valid("school", "division", "region", "account").required(),
|
|
5947
|
-
id: Joi18.string().hex().optional().allow("", null)
|
|
5948
|
-
});
|
|
5949
|
-
const { error } = validation.validate(payload);
|
|
6174
|
+
const { error } = schemaRole.validate(payload);
|
|
5950
6175
|
if (error) {
|
|
5951
|
-
next(new
|
|
6176
|
+
next(new BadRequestError30(error.message));
|
|
5952
6177
|
return;
|
|
5953
6178
|
}
|
|
5954
6179
|
try {
|
|
@@ -5963,22 +6188,22 @@ function useRoleController() {
|
|
|
5963
6188
|
const search = req.query.search ?? "";
|
|
5964
6189
|
const page = parseInt(req.query.page ?? "1");
|
|
5965
6190
|
const limit = parseInt(req.query.limit ?? "10");
|
|
5966
|
-
const
|
|
5967
|
-
const
|
|
6191
|
+
const app = req.query.app ?? "";
|
|
6192
|
+
const org = req.query.org ?? "";
|
|
5968
6193
|
const validation = Joi18.object({
|
|
5969
6194
|
search: Joi18.string().optional().allow("", null),
|
|
5970
6195
|
page: Joi18.number().required(),
|
|
5971
6196
|
limit: Joi18.number().required(),
|
|
5972
|
-
|
|
5973
|
-
|
|
6197
|
+
app: Joi18.string().optional().allow("", null),
|
|
6198
|
+
org: Joi18.string().hex().optional().allow("", null)
|
|
5974
6199
|
});
|
|
5975
|
-
const { error } = validation.validate({ search, page, limit,
|
|
6200
|
+
const { error } = validation.validate({ search, page, limit, app, org });
|
|
5976
6201
|
if (error) {
|
|
5977
|
-
next(new
|
|
6202
|
+
next(new BadRequestError30(error.message));
|
|
5978
6203
|
return;
|
|
5979
6204
|
}
|
|
5980
6205
|
try {
|
|
5981
|
-
const data = await _getRoles({ search, page, limit,
|
|
6206
|
+
const data = await _getRoles({ search, page, limit, app, org });
|
|
5982
6207
|
res.json(data);
|
|
5983
6208
|
return;
|
|
5984
6209
|
} catch (error2) {
|
|
@@ -5992,7 +6217,7 @@ function useRoleController() {
|
|
|
5992
6217
|
});
|
|
5993
6218
|
const { error } = validation.validate({ userId });
|
|
5994
6219
|
if (error) {
|
|
5995
|
-
next(new
|
|
6220
|
+
next(new BadRequestError30(error.message));
|
|
5996
6221
|
return;
|
|
5997
6222
|
}
|
|
5998
6223
|
try {
|
|
@@ -6010,11 +6235,11 @@ function useRoleController() {
|
|
|
6010
6235
|
});
|
|
6011
6236
|
const { error } = validation.validate({ _id });
|
|
6012
6237
|
if (error) {
|
|
6013
|
-
next(new
|
|
6238
|
+
next(new BadRequestError30(error.message));
|
|
6014
6239
|
return;
|
|
6015
6240
|
}
|
|
6016
6241
|
try {
|
|
6017
|
-
const data = await
|
|
6242
|
+
const data = await getById(_id);
|
|
6018
6243
|
res.json(data);
|
|
6019
6244
|
return;
|
|
6020
6245
|
} catch (error2) {
|
|
@@ -6032,7 +6257,7 @@ function useRoleController() {
|
|
|
6032
6257
|
});
|
|
6033
6258
|
const { error } = validation.validate({ _id, name, permissions });
|
|
6034
6259
|
if (error) {
|
|
6035
|
-
next(new
|
|
6260
|
+
next(new BadRequestError30(error.message));
|
|
6036
6261
|
return;
|
|
6037
6262
|
}
|
|
6038
6263
|
try {
|
|
@@ -6052,7 +6277,7 @@ function useRoleController() {
|
|
|
6052
6277
|
});
|
|
6053
6278
|
const { error } = validation.validate({ _id, permissions });
|
|
6054
6279
|
if (error) {
|
|
6055
|
-
next(new
|
|
6280
|
+
next(new BadRequestError30(error.message));
|
|
6056
6281
|
return;
|
|
6057
6282
|
}
|
|
6058
6283
|
try {
|
|
@@ -6063,7 +6288,6 @@ function useRoleController() {
|
|
|
6063
6288
|
next(error2);
|
|
6064
6289
|
}
|
|
6065
6290
|
}
|
|
6066
|
-
const { deleteById } = useRoleService();
|
|
6067
6291
|
async function deleteRole(req, res, next) {
|
|
6068
6292
|
const _id = req.params.id;
|
|
6069
6293
|
const validation = Joi18.object({
|
|
@@ -6071,11 +6295,11 @@ function useRoleController() {
|
|
|
6071
6295
|
});
|
|
6072
6296
|
const { error } = validation.validate({ _id });
|
|
6073
6297
|
if (error) {
|
|
6074
|
-
next(new
|
|
6298
|
+
next(new BadRequestError30(error.message));
|
|
6075
6299
|
return;
|
|
6076
6300
|
}
|
|
6077
6301
|
try {
|
|
6078
|
-
const message = await
|
|
6302
|
+
const message = await _deleteById(_id);
|
|
6079
6303
|
res.json({ message });
|
|
6080
6304
|
return;
|
|
6081
6305
|
} catch (error2) {
|
|
@@ -6095,8 +6319,8 @@ function useRoleController() {
|
|
|
6095
6319
|
|
|
6096
6320
|
// src/resources/verification/verification.controller.ts
|
|
6097
6321
|
import {
|
|
6098
|
-
AppError as
|
|
6099
|
-
BadRequestError as
|
|
6322
|
+
AppError as AppError13,
|
|
6323
|
+
BadRequestError as BadRequestError31,
|
|
6100
6324
|
InternalServerError as InternalServerError20
|
|
6101
6325
|
} from "@eeplatform/nodejs-utils";
|
|
6102
6326
|
import Joi19 from "joi";
|
|
@@ -6119,7 +6343,7 @@ function useVerificationController() {
|
|
|
6119
6343
|
});
|
|
6120
6344
|
const { error } = validation.validate(req.body);
|
|
6121
6345
|
if (error) {
|
|
6122
|
-
next(new
|
|
6346
|
+
next(new BadRequestError31(error.message));
|
|
6123
6347
|
return;
|
|
6124
6348
|
}
|
|
6125
6349
|
const email = req.body.email ?? "";
|
|
@@ -6150,7 +6374,7 @@ function useVerificationController() {
|
|
|
6150
6374
|
const validation = Joi19.string().email().required();
|
|
6151
6375
|
const { error } = validation.validate(email);
|
|
6152
6376
|
if (error) {
|
|
6153
|
-
next(new
|
|
6377
|
+
next(new BadRequestError31(error.message));
|
|
6154
6378
|
return;
|
|
6155
6379
|
}
|
|
6156
6380
|
try {
|
|
@@ -6160,7 +6384,7 @@ function useVerificationController() {
|
|
|
6160
6384
|
});
|
|
6161
6385
|
return;
|
|
6162
6386
|
} catch (error2) {
|
|
6163
|
-
if (error2 instanceof
|
|
6387
|
+
if (error2 instanceof AppError13) {
|
|
6164
6388
|
next(error2);
|
|
6165
6389
|
} else {
|
|
6166
6390
|
next(new InternalServerError20("An unexpected error occurred"));
|
|
@@ -6178,7 +6402,7 @@ function useVerificationController() {
|
|
|
6178
6402
|
});
|
|
6179
6403
|
const { error } = validation.validate(req.query);
|
|
6180
6404
|
if (error) {
|
|
6181
|
-
next(new
|
|
6405
|
+
next(new BadRequestError31(error.message));
|
|
6182
6406
|
return;
|
|
6183
6407
|
}
|
|
6184
6408
|
const status = req.query.status ?? "";
|
|
@@ -6212,7 +6436,7 @@ function useVerificationController() {
|
|
|
6212
6436
|
const validation = Joi19.string().hex().required();
|
|
6213
6437
|
const { error } = validation.validate(id);
|
|
6214
6438
|
if (error) {
|
|
6215
|
-
next(new
|
|
6439
|
+
next(new BadRequestError31(error.message));
|
|
6216
6440
|
return;
|
|
6217
6441
|
}
|
|
6218
6442
|
try {
|
|
@@ -6228,7 +6452,7 @@ function useVerificationController() {
|
|
|
6228
6452
|
const validation = Joi19.string().hex().required();
|
|
6229
6453
|
const { error } = validation.validate(otpId);
|
|
6230
6454
|
if (error) {
|
|
6231
|
-
next(new
|
|
6455
|
+
next(new BadRequestError31(error.message));
|
|
6232
6456
|
return;
|
|
6233
6457
|
}
|
|
6234
6458
|
try {
|
|
@@ -6250,7 +6474,7 @@ function useVerificationController() {
|
|
|
6250
6474
|
}
|
|
6251
6475
|
|
|
6252
6476
|
// src/resources/address/address.model.ts
|
|
6253
|
-
import { BadRequestError as
|
|
6477
|
+
import { BadRequestError as BadRequestError32 } from "@eeplatform/nodejs-utils";
|
|
6254
6478
|
import Joi20 from "joi";
|
|
6255
6479
|
import { ObjectId as ObjectId17 } from "mongodb";
|
|
6256
6480
|
var addressSchema = Joi20.object({
|
|
@@ -6270,14 +6494,14 @@ function MAddress(value) {
|
|
|
6270
6494
|
try {
|
|
6271
6495
|
value.user = new ObjectId17(value.user);
|
|
6272
6496
|
} catch (error) {
|
|
6273
|
-
throw new
|
|
6497
|
+
throw new BadRequestError32("Invalid user ID.");
|
|
6274
6498
|
}
|
|
6275
6499
|
}
|
|
6276
6500
|
if (value.org) {
|
|
6277
6501
|
try {
|
|
6278
6502
|
value.org = new ObjectId17(value.org);
|
|
6279
6503
|
} catch (error) {
|
|
6280
|
-
throw new
|
|
6504
|
+
throw new BadRequestError32("Invalid org ID.");
|
|
6281
6505
|
}
|
|
6282
6506
|
}
|
|
6283
6507
|
return {
|
|
@@ -6296,21 +6520,21 @@ function MAddress(value) {
|
|
|
6296
6520
|
|
|
6297
6521
|
// src/resources/address/address.repository.ts
|
|
6298
6522
|
import {
|
|
6299
|
-
BadRequestError as
|
|
6300
|
-
useAtlas as
|
|
6301
|
-
useCache as
|
|
6523
|
+
BadRequestError as BadRequestError33,
|
|
6524
|
+
useAtlas as useAtlas13,
|
|
6525
|
+
useCache as useCache9,
|
|
6302
6526
|
makeCacheKey as makeCacheKey12,
|
|
6303
6527
|
logger as logger20
|
|
6304
6528
|
} from "@eeplatform/nodejs-utils";
|
|
6305
6529
|
import { ObjectId as ObjectId18 } from "mongodb";
|
|
6306
6530
|
function useAddressRepo() {
|
|
6307
|
-
const db =
|
|
6531
|
+
const db = useAtlas13.getDb();
|
|
6308
6532
|
if (!db) {
|
|
6309
|
-
throw new
|
|
6533
|
+
throw new BadRequestError33("Unable to connect to server.");
|
|
6310
6534
|
}
|
|
6311
6535
|
const namespace_collection = "addresses";
|
|
6312
6536
|
const collection = db.collection(namespace_collection);
|
|
6313
|
-
const { getCache, setCache, delNamespace } =
|
|
6537
|
+
const { getCache, setCache, delNamespace } = useCache9(namespace_collection);
|
|
6314
6538
|
function delCachedData() {
|
|
6315
6539
|
delNamespace().then(() => {
|
|
6316
6540
|
logger20.log({
|
|
@@ -6332,7 +6556,7 @@ function useAddressRepo() {
|
|
|
6332
6556
|
{ key: { orgId: 1 } }
|
|
6333
6557
|
]);
|
|
6334
6558
|
} catch (error) {
|
|
6335
|
-
throw new
|
|
6559
|
+
throw new BadRequestError33("Failed to create index on address.");
|
|
6336
6560
|
}
|
|
6337
6561
|
}
|
|
6338
6562
|
async function add(value, session) {
|
|
@@ -6342,20 +6566,20 @@ function useAddressRepo() {
|
|
|
6342
6566
|
delCachedData();
|
|
6343
6567
|
return res.insertedId;
|
|
6344
6568
|
} catch (error) {
|
|
6345
|
-
throw new
|
|
6569
|
+
throw new BadRequestError33("Failed to create address.");
|
|
6346
6570
|
}
|
|
6347
6571
|
}
|
|
6348
6572
|
async function updateById(_id, value, session) {
|
|
6349
6573
|
try {
|
|
6350
6574
|
_id = new ObjectId18(_id);
|
|
6351
6575
|
} catch (error) {
|
|
6352
|
-
throw new
|
|
6576
|
+
throw new BadRequestError33("Invalid address ID.");
|
|
6353
6577
|
}
|
|
6354
6578
|
if (value.org) {
|
|
6355
6579
|
try {
|
|
6356
6580
|
value.org = new ObjectId18(value.org);
|
|
6357
6581
|
} catch (error) {
|
|
6358
|
-
throw new
|
|
6582
|
+
throw new BadRequestError33("Invalid org ID.");
|
|
6359
6583
|
}
|
|
6360
6584
|
}
|
|
6361
6585
|
try {
|
|
@@ -6367,14 +6591,14 @@ function useAddressRepo() {
|
|
|
6367
6591
|
delCachedData();
|
|
6368
6592
|
return "Successfully updated address.";
|
|
6369
6593
|
} catch (error) {
|
|
6370
|
-
throw new
|
|
6594
|
+
throw new BadRequestError33("Failed to update address.");
|
|
6371
6595
|
}
|
|
6372
6596
|
}
|
|
6373
6597
|
async function getByUserId(user) {
|
|
6374
6598
|
try {
|
|
6375
6599
|
user = new ObjectId18(user);
|
|
6376
6600
|
} catch (error) {
|
|
6377
|
-
throw new
|
|
6601
|
+
throw new BadRequestError33("Invalid user ID.");
|
|
6378
6602
|
}
|
|
6379
6603
|
const cacheKey = makeCacheKey12(namespace_collection, { user: String(user) });
|
|
6380
6604
|
try {
|
|
@@ -6402,14 +6626,14 @@ function useAddressRepo() {
|
|
|
6402
6626
|
}
|
|
6403
6627
|
return data;
|
|
6404
6628
|
} catch (error) {
|
|
6405
|
-
throw new
|
|
6629
|
+
throw new BadRequestError33("Failed to get address by ID.");
|
|
6406
6630
|
}
|
|
6407
6631
|
}
|
|
6408
6632
|
async function getByOrgId(org) {
|
|
6409
6633
|
try {
|
|
6410
6634
|
org = new ObjectId18(org);
|
|
6411
6635
|
} catch (error) {
|
|
6412
|
-
throw new
|
|
6636
|
+
throw new BadRequestError33("Invalid orgId.");
|
|
6413
6637
|
}
|
|
6414
6638
|
const cacheKey = makeCacheKey12(namespace_collection, { org: String(org) });
|
|
6415
6639
|
try {
|
|
@@ -6437,7 +6661,7 @@ function useAddressRepo() {
|
|
|
6437
6661
|
}
|
|
6438
6662
|
return data;
|
|
6439
6663
|
} catch (error) {
|
|
6440
|
-
throw new
|
|
6664
|
+
throw new BadRequestError33("Failed to get address by orgId.");
|
|
6441
6665
|
}
|
|
6442
6666
|
}
|
|
6443
6667
|
return {
|
|
@@ -6450,7 +6674,7 @@ function useAddressRepo() {
|
|
|
6450
6674
|
}
|
|
6451
6675
|
|
|
6452
6676
|
// src/resources/address/address.controller.ts
|
|
6453
|
-
import { BadRequestError as
|
|
6677
|
+
import { BadRequestError as BadRequestError34, NotFoundError as NotFoundError4 } from "@eeplatform/nodejs-utils";
|
|
6454
6678
|
import Joi21 from "joi";
|
|
6455
6679
|
function useAddressController() {
|
|
6456
6680
|
const {
|
|
@@ -6475,7 +6699,7 @@ function useAddressController() {
|
|
|
6475
6699
|
});
|
|
6476
6700
|
const { error } = validation.validate(value);
|
|
6477
6701
|
if (error) {
|
|
6478
|
-
next(new
|
|
6702
|
+
next(new BadRequestError34(error.message));
|
|
6479
6703
|
}
|
|
6480
6704
|
try {
|
|
6481
6705
|
const value2 = req.body;
|
|
@@ -6502,7 +6726,7 @@ function useAddressController() {
|
|
|
6502
6726
|
});
|
|
6503
6727
|
const { error } = validation.validate({ id, ...value });
|
|
6504
6728
|
if (error) {
|
|
6505
|
-
next(new
|
|
6729
|
+
next(new BadRequestError34(error.message));
|
|
6506
6730
|
return;
|
|
6507
6731
|
}
|
|
6508
6732
|
try {
|
|
@@ -6518,7 +6742,7 @@ function useAddressController() {
|
|
|
6518
6742
|
const validation = Joi21.string().hex().required();
|
|
6519
6743
|
const { error } = validation.validate(user);
|
|
6520
6744
|
if (error) {
|
|
6521
|
-
next(new
|
|
6745
|
+
next(new BadRequestError34(error.message));
|
|
6522
6746
|
}
|
|
6523
6747
|
try {
|
|
6524
6748
|
const address = await _getByUserId(user);
|
|
@@ -6537,7 +6761,7 @@ function useAddressController() {
|
|
|
6537
6761
|
const validation = Joi21.string().hex().required();
|
|
6538
6762
|
const { error } = validation.validate(id);
|
|
6539
6763
|
if (error) {
|
|
6540
|
-
next(new
|
|
6764
|
+
next(new BadRequestError34(error.message));
|
|
6541
6765
|
}
|
|
6542
6766
|
try {
|
|
6543
6767
|
const address = await _getByOrgId(id);
|
|
@@ -6570,7 +6794,7 @@ var MToken = class {
|
|
|
6570
6794
|
};
|
|
6571
6795
|
|
|
6572
6796
|
// src/resources/counter/counter.model.ts
|
|
6573
|
-
import { BadRequestError as
|
|
6797
|
+
import { BadRequestError as BadRequestError35 } from "@eeplatform/nodejs-utils";
|
|
6574
6798
|
import { ObjectId as ObjectId20 } from "mongodb";
|
|
6575
6799
|
import { z } from "zod";
|
|
6576
6800
|
var TCounter = z.object({
|
|
@@ -6590,7 +6814,7 @@ function useCounterModel(db) {
|
|
|
6590
6814
|
try {
|
|
6591
6815
|
return TCounter.parse(value);
|
|
6592
6816
|
} catch (error) {
|
|
6593
|
-
throw new
|
|
6817
|
+
throw new BadRequestError35(error.issues[0].message);
|
|
6594
6818
|
}
|
|
6595
6819
|
}
|
|
6596
6820
|
function validateCounter(data) {
|
|
@@ -6605,19 +6829,19 @@ function useCounterModel(db) {
|
|
|
6605
6829
|
|
|
6606
6830
|
// src/resources/counter/counter.repository.ts
|
|
6607
6831
|
import {
|
|
6608
|
-
useAtlas as
|
|
6609
|
-
useCache as
|
|
6832
|
+
useAtlas as useAtlas14,
|
|
6833
|
+
useCache as useCache10,
|
|
6610
6834
|
makeCacheKey as makeCacheKey13,
|
|
6611
6835
|
logger as logger21
|
|
6612
6836
|
} from "@eeplatform/nodejs-utils";
|
|
6613
6837
|
function useCounterRepo() {
|
|
6614
|
-
const db =
|
|
6838
|
+
const db = useAtlas14.getDb();
|
|
6615
6839
|
if (!db) {
|
|
6616
6840
|
throw new Error("Unable to connect to server.");
|
|
6617
6841
|
}
|
|
6618
6842
|
const namespace_collection = "counters";
|
|
6619
6843
|
const { collection, createCounter } = useCounterModel(db);
|
|
6620
|
-
const { getCache, setCache, delNamespace } =
|
|
6844
|
+
const { getCache, setCache, delNamespace } = useCache10(namespace_collection);
|
|
6621
6845
|
function delCachedData() {
|
|
6622
6846
|
delNamespace().then(() => {
|
|
6623
6847
|
logger21.log({
|
|
@@ -7033,7 +7257,7 @@ IMPORTANT: Only consider it a match if the target phoneme is the FIRST sound pro
|
|
|
7033
7257
|
}
|
|
7034
7258
|
|
|
7035
7259
|
// src/resources/utils/github.service.ts
|
|
7036
|
-
import { AppError as
|
|
7260
|
+
import { AppError as AppError14, BadRequestError as BadRequestError36 } from "@eeplatform/nodejs-utils";
|
|
7037
7261
|
import { Octokit } from "@octokit/rest";
|
|
7038
7262
|
import _sodium from "libsodium-wrappers";
|
|
7039
7263
|
function useGitHubService() {
|
|
@@ -7047,23 +7271,23 @@ function useGitHubService() {
|
|
|
7047
7271
|
try {
|
|
7048
7272
|
const { data: repoData } = await octokit.repos.get({ owner, repo });
|
|
7049
7273
|
if (!repoData.permissions?.admin) {
|
|
7050
|
-
throw new
|
|
7274
|
+
throw new BadRequestError36(
|
|
7051
7275
|
"You do not have admin access to this repository."
|
|
7052
7276
|
);
|
|
7053
7277
|
}
|
|
7054
7278
|
} catch (error) {
|
|
7055
7279
|
if (error.status === 404) {
|
|
7056
|
-
throw new
|
|
7280
|
+
throw new BadRequestError36(
|
|
7057
7281
|
"Repository not found or you don't have access to it."
|
|
7058
7282
|
);
|
|
7059
7283
|
} else if (error.status === 401) {
|
|
7060
|
-
throw new
|
|
7284
|
+
throw new BadRequestError36(
|
|
7061
7285
|
"Invalid GitHub token or insufficient permissions."
|
|
7062
7286
|
);
|
|
7063
7287
|
} else if (error.message.includes("admin access")) {
|
|
7064
7288
|
throw error;
|
|
7065
7289
|
} else {
|
|
7066
|
-
throw new
|
|
7290
|
+
throw new BadRequestError36(
|
|
7067
7291
|
`Failed to check repository permissions: ${error.message}`
|
|
7068
7292
|
);
|
|
7069
7293
|
}
|
|
@@ -7112,7 +7336,7 @@ function useGitHubService() {
|
|
|
7112
7336
|
key_id: publicKeyRes.key_id
|
|
7113
7337
|
});
|
|
7114
7338
|
} catch (encryptionError) {
|
|
7115
|
-
throw new
|
|
7339
|
+
throw new BadRequestError36(
|
|
7116
7340
|
`Failed to encrypt secret '${key}': ${encryptionError.message}`
|
|
7117
7341
|
);
|
|
7118
7342
|
}
|
|
@@ -7128,22 +7352,22 @@ function useGitHubService() {
|
|
|
7128
7352
|
}
|
|
7129
7353
|
return `Successfully set ${lines.length} ${type} variables/secrets in environment '${environment}'`;
|
|
7130
7354
|
} catch (error) {
|
|
7131
|
-
if (error instanceof
|
|
7355
|
+
if (error instanceof AppError14)
|
|
7132
7356
|
throw error;
|
|
7133
7357
|
if (error.status === 422) {
|
|
7134
|
-
throw new
|
|
7358
|
+
throw new BadRequestError36(
|
|
7135
7359
|
`GitHub API validation error: ${error.message}`
|
|
7136
7360
|
);
|
|
7137
7361
|
} else if (error.status === 404) {
|
|
7138
|
-
throw new
|
|
7362
|
+
throw new BadRequestError36("Environment or repository not found.");
|
|
7139
7363
|
} else if (error.status === 403) {
|
|
7140
|
-
throw new
|
|
7364
|
+
throw new BadRequestError36(
|
|
7141
7365
|
"Forbidden: Insufficient permissions or rate limit exceeded."
|
|
7142
7366
|
);
|
|
7143
7367
|
} else if (error.message.includes("admin access") || error.message.includes("permissions")) {
|
|
7144
7368
|
throw error;
|
|
7145
7369
|
} else {
|
|
7146
|
-
throw new
|
|
7370
|
+
throw new BadRequestError36(
|
|
7147
7371
|
`Failed to set GitHub variables: ${error.message}`
|
|
7148
7372
|
);
|
|
7149
7373
|
}
|
|
@@ -7155,7 +7379,7 @@ function useGitHubService() {
|
|
|
7155
7379
|
}
|
|
7156
7380
|
|
|
7157
7381
|
// src/resources/utils/transcribe.service.ts
|
|
7158
|
-
import { AppError as
|
|
7382
|
+
import { AppError as AppError15, BadRequestError as BadRequestError37, useRedis } from "@eeplatform/nodejs-utils";
|
|
7159
7383
|
function useTranscribeService() {
|
|
7160
7384
|
const { createFile } = useFileService();
|
|
7161
7385
|
const { getClient } = useRedis();
|
|
@@ -7164,10 +7388,10 @@ function useTranscribeService() {
|
|
|
7164
7388
|
try {
|
|
7165
7389
|
const fileId = await createFile(file);
|
|
7166
7390
|
} catch (error) {
|
|
7167
|
-
if (error instanceof
|
|
7391
|
+
if (error instanceof AppError15) {
|
|
7168
7392
|
throw error;
|
|
7169
7393
|
} else {
|
|
7170
|
-
throw new
|
|
7394
|
+
throw new BadRequestError37("Failed to transcribe audio file");
|
|
7171
7395
|
}
|
|
7172
7396
|
}
|
|
7173
7397
|
}
|
|
@@ -7178,8 +7402,8 @@ function useTranscribeService() {
|
|
|
7178
7402
|
|
|
7179
7403
|
// src/resources/utils/audio-transcription.controller.ts
|
|
7180
7404
|
import {
|
|
7181
|
-
AppError as
|
|
7182
|
-
BadRequestError as
|
|
7405
|
+
AppError as AppError16,
|
|
7406
|
+
BadRequestError as BadRequestError38,
|
|
7183
7407
|
InternalServerError as InternalServerError21
|
|
7184
7408
|
} from "@eeplatform/nodejs-utils";
|
|
7185
7409
|
import Joi22 from "joi";
|
|
@@ -7202,7 +7426,7 @@ function useAudioTranscriptionController() {
|
|
|
7202
7426
|
});
|
|
7203
7427
|
async function transcribeFromFile(req, res, next) {
|
|
7204
7428
|
if (!req.file) {
|
|
7205
|
-
next(new
|
|
7429
|
+
next(new BadRequestError38("Audio file is required"));
|
|
7206
7430
|
return;
|
|
7207
7431
|
}
|
|
7208
7432
|
try {
|
|
@@ -7210,14 +7434,14 @@ function useAudioTranscriptionController() {
|
|
|
7210
7434
|
req.body
|
|
7211
7435
|
);
|
|
7212
7436
|
if (error) {
|
|
7213
|
-
next(new
|
|
7437
|
+
next(new BadRequestError38(error.message));
|
|
7214
7438
|
return;
|
|
7215
7439
|
}
|
|
7216
7440
|
const { buffer, mimetype, originalname, size } = req.file;
|
|
7217
7441
|
if (!geminiService.validateAudioFormat(mimetype)) {
|
|
7218
7442
|
const supportedFormats = geminiService.getSupportedAudioFormats();
|
|
7219
7443
|
next(
|
|
7220
|
-
new
|
|
7444
|
+
new BadRequestError38(
|
|
7221
7445
|
`Unsupported audio format: ${mimetype}. Supported formats: ${supportedFormats.join(
|
|
7222
7446
|
", "
|
|
7223
7447
|
)}`
|
|
@@ -7228,7 +7452,7 @@ function useAudioTranscriptionController() {
|
|
|
7228
7452
|
const maxSizeBytes = 25 * 1024 * 1024;
|
|
7229
7453
|
if (size > maxSizeBytes) {
|
|
7230
7454
|
next(
|
|
7231
|
-
new
|
|
7455
|
+
new BadRequestError38(
|
|
7232
7456
|
`File size too large. Maximum allowed size is ${maxSizeBytes / (1024 * 1024)}MB`
|
|
7233
7457
|
)
|
|
7234
7458
|
);
|
|
@@ -7258,7 +7482,7 @@ function useAudioTranscriptionController() {
|
|
|
7258
7482
|
}
|
|
7259
7483
|
});
|
|
7260
7484
|
} catch (error) {
|
|
7261
|
-
if (error instanceof
|
|
7485
|
+
if (error instanceof AppError16) {
|
|
7262
7486
|
next(error);
|
|
7263
7487
|
} else {
|
|
7264
7488
|
next(
|
|
@@ -7278,14 +7502,14 @@ function useAudioTranscriptionController() {
|
|
|
7278
7502
|
});
|
|
7279
7503
|
const { error, value } = bodySchema.validate(req.body);
|
|
7280
7504
|
if (error) {
|
|
7281
|
-
next(new
|
|
7505
|
+
next(new BadRequestError38(error.message));
|
|
7282
7506
|
return;
|
|
7283
7507
|
}
|
|
7284
7508
|
const { audioData, mimeType, options = {} } = value;
|
|
7285
7509
|
if (!geminiService.validateAudioFormat(mimeType)) {
|
|
7286
7510
|
const supportedFormats = geminiService.getSupportedAudioFormats();
|
|
7287
7511
|
next(
|
|
7288
|
-
new
|
|
7512
|
+
new BadRequestError38(
|
|
7289
7513
|
`Unsupported audio format: ${mimeType}. Supported formats: ${supportedFormats.join(
|
|
7290
7514
|
", "
|
|
7291
7515
|
)}`
|
|
@@ -7297,13 +7521,13 @@ function useAudioTranscriptionController() {
|
|
|
7297
7521
|
try {
|
|
7298
7522
|
audioBuffer = Buffer.from(audioData, "base64");
|
|
7299
7523
|
} catch (conversionError) {
|
|
7300
|
-
next(new
|
|
7524
|
+
next(new BadRequestError38("Invalid base64 audio data"));
|
|
7301
7525
|
return;
|
|
7302
7526
|
}
|
|
7303
7527
|
const maxSizeBytes = 25 * 1024 * 1024;
|
|
7304
7528
|
if (audioBuffer.length > maxSizeBytes) {
|
|
7305
7529
|
next(
|
|
7306
|
-
new
|
|
7530
|
+
new BadRequestError38(
|
|
7307
7531
|
`Audio data too large. Maximum allowed size is ${maxSizeBytes / (1024 * 1024)}MB`
|
|
7308
7532
|
)
|
|
7309
7533
|
);
|
|
@@ -7332,7 +7556,7 @@ function useAudioTranscriptionController() {
|
|
|
7332
7556
|
}
|
|
7333
7557
|
});
|
|
7334
7558
|
} catch (error) {
|
|
7335
|
-
if (error instanceof
|
|
7559
|
+
if (error instanceof AppError16) {
|
|
7336
7560
|
next(error);
|
|
7337
7561
|
} else {
|
|
7338
7562
|
next(
|
|
@@ -7369,7 +7593,7 @@ function useAudioTranscriptionController() {
|
|
|
7369
7593
|
});
|
|
7370
7594
|
const { error, value } = schema.validate(req.body);
|
|
7371
7595
|
if (error) {
|
|
7372
|
-
next(new
|
|
7596
|
+
next(new BadRequestError38(error.message));
|
|
7373
7597
|
return;
|
|
7374
7598
|
}
|
|
7375
7599
|
const { mimeType } = value;
|
|
@@ -7391,13 +7615,13 @@ function useAudioTranscriptionController() {
|
|
|
7391
7615
|
}
|
|
7392
7616
|
async function checkPhonemeFromFile(req, res, next) {
|
|
7393
7617
|
if (!req.file) {
|
|
7394
|
-
next(new
|
|
7618
|
+
next(new BadRequestError38("Audio file is required"));
|
|
7395
7619
|
return;
|
|
7396
7620
|
}
|
|
7397
7621
|
try {
|
|
7398
7622
|
const { error, value } = phonemeMatchSchema.validate(req.body);
|
|
7399
7623
|
if (error) {
|
|
7400
|
-
next(new
|
|
7624
|
+
next(new BadRequestError38(error.message));
|
|
7401
7625
|
return;
|
|
7402
7626
|
}
|
|
7403
7627
|
const { targetPhoneme, options = {} } = value;
|
|
@@ -7405,7 +7629,7 @@ function useAudioTranscriptionController() {
|
|
|
7405
7629
|
if (!geminiService.validateAudioFormat(mimetype)) {
|
|
7406
7630
|
const supportedFormats = geminiService.getSupportedAudioFormats();
|
|
7407
7631
|
next(
|
|
7408
|
-
new
|
|
7632
|
+
new BadRequestError38(
|
|
7409
7633
|
`Unsupported audio format: ${mimetype}. Supported formats: ${supportedFormats.join(
|
|
7410
7634
|
", "
|
|
7411
7635
|
)}`
|
|
@@ -7416,7 +7640,7 @@ function useAudioTranscriptionController() {
|
|
|
7416
7640
|
const maxSizeBytes = 25 * 1024 * 1024;
|
|
7417
7641
|
if (size > maxSizeBytes) {
|
|
7418
7642
|
next(
|
|
7419
|
-
new
|
|
7643
|
+
new BadRequestError38(
|
|
7420
7644
|
`File size too large. Maximum allowed size is ${maxSizeBytes / (1024 * 1024)}MB`
|
|
7421
7645
|
)
|
|
7422
7646
|
);
|
|
@@ -7431,7 +7655,7 @@ function useAudioTranscriptionController() {
|
|
|
7431
7655
|
);
|
|
7432
7656
|
const processingTime = Date.now() - startTime;
|
|
7433
7657
|
if (!result.isMatch) {
|
|
7434
|
-
throw new
|
|
7658
|
+
throw new BadRequestError38(
|
|
7435
7659
|
`Phoneme "${targetPhoneme}" not found in the provided audio. Transcription: "${result.transcription}"`
|
|
7436
7660
|
);
|
|
7437
7661
|
}
|
|
@@ -7459,7 +7683,7 @@ function useAudioTranscriptionController() {
|
|
|
7459
7683
|
}
|
|
7460
7684
|
});
|
|
7461
7685
|
} catch (error) {
|
|
7462
|
-
if (error instanceof
|
|
7686
|
+
if (error instanceof AppError16) {
|
|
7463
7687
|
next(error);
|
|
7464
7688
|
} else {
|
|
7465
7689
|
next(
|
|
@@ -7478,14 +7702,14 @@ function useAudioTranscriptionController() {
|
|
|
7478
7702
|
});
|
|
7479
7703
|
const { error, value } = bodySchema.validate(req.body);
|
|
7480
7704
|
if (error) {
|
|
7481
|
-
next(new
|
|
7705
|
+
next(new BadRequestError38(error.message));
|
|
7482
7706
|
return;
|
|
7483
7707
|
}
|
|
7484
7708
|
const { audioData, mimeType, targetPhoneme, options = {} } = value;
|
|
7485
7709
|
if (!geminiService.validateAudioFormat(mimeType)) {
|
|
7486
7710
|
const supportedFormats = geminiService.getSupportedAudioFormats();
|
|
7487
7711
|
next(
|
|
7488
|
-
new
|
|
7712
|
+
new BadRequestError38(
|
|
7489
7713
|
`Unsupported audio format: ${mimeType}. Supported formats: ${supportedFormats.join(
|
|
7490
7714
|
", "
|
|
7491
7715
|
)}`
|
|
@@ -7497,13 +7721,13 @@ function useAudioTranscriptionController() {
|
|
|
7497
7721
|
try {
|
|
7498
7722
|
audioBuffer = Buffer.from(audioData, "base64");
|
|
7499
7723
|
} catch (conversionError) {
|
|
7500
|
-
next(new
|
|
7724
|
+
next(new BadRequestError38("Invalid base64 audio data"));
|
|
7501
7725
|
return;
|
|
7502
7726
|
}
|
|
7503
7727
|
const maxSizeBytes = 25 * 1024 * 1024;
|
|
7504
7728
|
if (audioBuffer.length > maxSizeBytes) {
|
|
7505
7729
|
next(
|
|
7506
|
-
new
|
|
7730
|
+
new BadRequestError38(
|
|
7507
7731
|
`Audio data too large. Maximum allowed size is ${maxSizeBytes / (1024 * 1024)}MB`
|
|
7508
7732
|
)
|
|
7509
7733
|
);
|
|
@@ -7518,7 +7742,7 @@ function useAudioTranscriptionController() {
|
|
|
7518
7742
|
);
|
|
7519
7743
|
const processingTime = Date.now() - startTime;
|
|
7520
7744
|
if (!result.isMatch) {
|
|
7521
|
-
throw new
|
|
7745
|
+
throw new BadRequestError38(
|
|
7522
7746
|
`Phoneme "${targetPhoneme}" not found in the provided audio. Transcription: "${result.transcription}"`
|
|
7523
7747
|
);
|
|
7524
7748
|
}
|
|
@@ -7545,7 +7769,7 @@ function useAudioTranscriptionController() {
|
|
|
7545
7769
|
}
|
|
7546
7770
|
});
|
|
7547
7771
|
} catch (error) {
|
|
7548
|
-
if (error instanceof
|
|
7772
|
+
if (error instanceof AppError16) {
|
|
7549
7773
|
next(error);
|
|
7550
7774
|
} else {
|
|
7551
7775
|
next(
|
|
@@ -7556,7 +7780,7 @@ function useAudioTranscriptionController() {
|
|
|
7556
7780
|
}
|
|
7557
7781
|
async function batchPhonemeCheck(req, res, next) {
|
|
7558
7782
|
if (!req.file) {
|
|
7559
|
-
next(new
|
|
7783
|
+
next(new BadRequestError38("Audio file is required"));
|
|
7560
7784
|
return;
|
|
7561
7785
|
}
|
|
7562
7786
|
try {
|
|
@@ -7566,7 +7790,7 @@ function useAudioTranscriptionController() {
|
|
|
7566
7790
|
});
|
|
7567
7791
|
const { error, value } = batchSchema.validate(req.body);
|
|
7568
7792
|
if (error) {
|
|
7569
|
-
next(new
|
|
7793
|
+
next(new BadRequestError38(error.message));
|
|
7570
7794
|
return;
|
|
7571
7795
|
}
|
|
7572
7796
|
const { targetPhonemes, options = {} } = value;
|
|
@@ -7574,7 +7798,7 @@ function useAudioTranscriptionController() {
|
|
|
7574
7798
|
if (!geminiService.validateAudioFormat(mimetype)) {
|
|
7575
7799
|
const supportedFormats = geminiService.getSupportedAudioFormats();
|
|
7576
7800
|
next(
|
|
7577
|
-
new
|
|
7801
|
+
new BadRequestError38(
|
|
7578
7802
|
`Unsupported audio format: ${mimetype}. Supported formats: ${supportedFormats.join(
|
|
7579
7803
|
", "
|
|
7580
7804
|
)}`
|
|
@@ -7585,7 +7809,7 @@ function useAudioTranscriptionController() {
|
|
|
7585
7809
|
const maxSizeBytes = 25 * 1024 * 1024;
|
|
7586
7810
|
if (size > maxSizeBytes) {
|
|
7587
7811
|
next(
|
|
7588
|
-
new
|
|
7812
|
+
new BadRequestError38(
|
|
7589
7813
|
`File size too large. Maximum allowed size is ${maxSizeBytes / (1024 * 1024)}MB`
|
|
7590
7814
|
)
|
|
7591
7815
|
);
|
|
@@ -7641,7 +7865,7 @@ function useAudioTranscriptionController() {
|
|
|
7641
7865
|
}
|
|
7642
7866
|
});
|
|
7643
7867
|
} catch (error) {
|
|
7644
|
-
if (error instanceof
|
|
7868
|
+
if (error instanceof AppError16) {
|
|
7645
7869
|
next(error);
|
|
7646
7870
|
} else {
|
|
7647
7871
|
next(
|
|
@@ -7684,8 +7908,8 @@ function useAudioTranscriptionController() {
|
|
|
7684
7908
|
// src/resources/utils/util.controller.ts
|
|
7685
7909
|
import Joi23 from "joi";
|
|
7686
7910
|
import {
|
|
7687
|
-
AppError as
|
|
7688
|
-
BadRequestError as
|
|
7911
|
+
AppError as AppError17,
|
|
7912
|
+
BadRequestError as BadRequestError39,
|
|
7689
7913
|
InternalServerError as InternalServerError22,
|
|
7690
7914
|
logger as logger22
|
|
7691
7915
|
} from "@eeplatform/nodejs-utils";
|
|
@@ -7742,13 +7966,13 @@ function useUtilController() {
|
|
|
7742
7966
|
keyValues
|
|
7743
7967
|
});
|
|
7744
7968
|
if (error) {
|
|
7745
|
-
next(new
|
|
7969
|
+
next(new BadRequestError39(error.message));
|
|
7746
7970
|
return;
|
|
7747
7971
|
}
|
|
7748
7972
|
const repoUrlPattern = /github\.com[:\/]([^\/]+)\/(.+)\.git$/;
|
|
7749
7973
|
if (!repoUrlPattern.test(repoUrl)) {
|
|
7750
7974
|
next(
|
|
7751
|
-
new
|
|
7975
|
+
new BadRequestError39(
|
|
7752
7976
|
"Invalid GitHub repository URL format. Expected format: https://github.com/owner/repo.git"
|
|
7753
7977
|
)
|
|
7754
7978
|
);
|
|
@@ -7760,7 +7984,7 @@ function useUtilController() {
|
|
|
7760
7984
|
);
|
|
7761
7985
|
if (invalidLines.length > 0) {
|
|
7762
7986
|
next(
|
|
7763
|
-
new
|
|
7987
|
+
new BadRequestError39(
|
|
7764
7988
|
"Invalid key-value format. Each pair should be in format: KEY=value. Pairs should be separated by semicolons."
|
|
7765
7989
|
)
|
|
7766
7990
|
);
|
|
@@ -7795,7 +8019,7 @@ function useUtilController() {
|
|
|
7795
8019
|
error: error.message,
|
|
7796
8020
|
stack: error.stack
|
|
7797
8021
|
});
|
|
7798
|
-
if (error instanceof
|
|
8022
|
+
if (error instanceof AppError17) {
|
|
7799
8023
|
next(error);
|
|
7800
8024
|
} else {
|
|
7801
8025
|
next(
|
|
@@ -7856,24 +8080,24 @@ function modelPSGC(data) {
|
|
|
7856
8080
|
|
|
7857
8081
|
// src/resources/psgc/psgc.repository.ts
|
|
7858
8082
|
import {
|
|
7859
|
-
AppError as
|
|
7860
|
-
BadRequestError as
|
|
8083
|
+
AppError as AppError18,
|
|
8084
|
+
BadRequestError as BadRequestError40,
|
|
7861
8085
|
InternalServerError as InternalServerError23,
|
|
7862
8086
|
logger as logger23,
|
|
7863
8087
|
makeCacheKey as makeCacheKey14,
|
|
7864
8088
|
paginate as paginate9,
|
|
7865
|
-
useAtlas as
|
|
7866
|
-
useCache as
|
|
8089
|
+
useAtlas as useAtlas15,
|
|
8090
|
+
useCache as useCache11
|
|
7867
8091
|
} from "@eeplatform/nodejs-utils";
|
|
7868
8092
|
import { ObjectId as ObjectId21 } from "mongodb";
|
|
7869
8093
|
function usePSGCRepo() {
|
|
7870
|
-
const db =
|
|
8094
|
+
const db = useAtlas15.getDb();
|
|
7871
8095
|
if (!db) {
|
|
7872
8096
|
throw new Error("Unable to connect to server.");
|
|
7873
8097
|
}
|
|
7874
8098
|
const namespace_collection = "psgc";
|
|
7875
8099
|
const collection = db.collection(namespace_collection);
|
|
7876
|
-
const { getCache, setCache, delNamespace } =
|
|
8100
|
+
const { getCache, setCache, delNamespace } = useCache11(namespace_collection);
|
|
7877
8101
|
async function createIndexes() {
|
|
7878
8102
|
try {
|
|
7879
8103
|
await collection.createIndexes([
|
|
@@ -7909,12 +8133,12 @@ function usePSGCRepo() {
|
|
|
7909
8133
|
level: "error",
|
|
7910
8134
|
message: error.message
|
|
7911
8135
|
});
|
|
7912
|
-
if (error instanceof
|
|
8136
|
+
if (error instanceof AppError18) {
|
|
7913
8137
|
throw error;
|
|
7914
8138
|
} else {
|
|
7915
8139
|
const isDuplicated = error.message.includes("duplicate");
|
|
7916
8140
|
if (isDuplicated) {
|
|
7917
|
-
throw new
|
|
8141
|
+
throw new BadRequestError40("Region already exists.");
|
|
7918
8142
|
}
|
|
7919
8143
|
throw new Error("Failed to create PSGC.");
|
|
7920
8144
|
}
|
|
@@ -7993,7 +8217,7 @@ function usePSGCRepo() {
|
|
|
7993
8217
|
try {
|
|
7994
8218
|
_id = new ObjectId21(_id);
|
|
7995
8219
|
} catch (error) {
|
|
7996
|
-
throw new
|
|
8220
|
+
throw new BadRequestError40("Invalid ID.");
|
|
7997
8221
|
}
|
|
7998
8222
|
const cacheKey = makeCacheKey14(namespace_collection, { _id: String(_id) });
|
|
7999
8223
|
try {
|
|
@@ -8010,7 +8234,7 @@ function usePSGCRepo() {
|
|
|
8010
8234
|
deletedAt: { $in: ["", null] }
|
|
8011
8235
|
});
|
|
8012
8236
|
if (!result) {
|
|
8013
|
-
throw new
|
|
8237
|
+
throw new BadRequestError40("Region not found.");
|
|
8014
8238
|
}
|
|
8015
8239
|
setCache(cacheKey, result, 300).then(() => {
|
|
8016
8240
|
logger23.log({
|
|
@@ -8025,7 +8249,7 @@ function usePSGCRepo() {
|
|
|
8025
8249
|
});
|
|
8026
8250
|
return result;
|
|
8027
8251
|
} catch (error) {
|
|
8028
|
-
if (error instanceof
|
|
8252
|
+
if (error instanceof AppError18) {
|
|
8029
8253
|
throw error;
|
|
8030
8254
|
} else {
|
|
8031
8255
|
throw new InternalServerError23("Failed to get PSGC.");
|
|
@@ -8080,7 +8304,7 @@ function usePSGCRepo() {
|
|
|
8080
8304
|
});
|
|
8081
8305
|
return result;
|
|
8082
8306
|
} catch (error) {
|
|
8083
|
-
if (error instanceof
|
|
8307
|
+
if (error instanceof AppError18) {
|
|
8084
8308
|
throw error;
|
|
8085
8309
|
} else {
|
|
8086
8310
|
throw new InternalServerError23("Failed to get PSGC.");
|
|
@@ -8090,14 +8314,14 @@ function usePSGCRepo() {
|
|
|
8090
8314
|
async function updateFieldById({ _id, field, value } = {}, session) {
|
|
8091
8315
|
const allowedFields = ["name"];
|
|
8092
8316
|
if (!allowedFields.includes(field)) {
|
|
8093
|
-
throw new
|
|
8317
|
+
throw new BadRequestError40(
|
|
8094
8318
|
`Field "${field}" is not allowed to be updated.`
|
|
8095
8319
|
);
|
|
8096
8320
|
}
|
|
8097
8321
|
try {
|
|
8098
8322
|
_id = new ObjectId21(_id);
|
|
8099
8323
|
} catch (error) {
|
|
8100
|
-
throw new
|
|
8324
|
+
throw new BadRequestError40("Invalid ID.");
|
|
8101
8325
|
}
|
|
8102
8326
|
try {
|
|
8103
8327
|
await collection.updateOne(
|
|
@@ -8115,7 +8339,7 @@ function usePSGCRepo() {
|
|
|
8115
8339
|
try {
|
|
8116
8340
|
_id = new ObjectId21(_id);
|
|
8117
8341
|
} catch (error) {
|
|
8118
|
-
throw new
|
|
8342
|
+
throw new BadRequestError40("Invalid ID.");
|
|
8119
8343
|
}
|
|
8120
8344
|
try {
|
|
8121
8345
|
await collection.updateOne(
|
|
@@ -8175,7 +8399,7 @@ function usePSGCRepo() {
|
|
|
8175
8399
|
}
|
|
8176
8400
|
|
|
8177
8401
|
// src/resources/psgc/psgc.controller.ts
|
|
8178
|
-
import { BadRequestError as
|
|
8402
|
+
import { BadRequestError as BadRequestError41 } from "@eeplatform/nodejs-utils";
|
|
8179
8403
|
import Joi26 from "joi";
|
|
8180
8404
|
function usePSGCController() {
|
|
8181
8405
|
const {
|
|
@@ -8190,7 +8414,7 @@ function usePSGCController() {
|
|
|
8190
8414
|
const value = req.body;
|
|
8191
8415
|
const { error } = schemaPSGC.validate(value);
|
|
8192
8416
|
if (error) {
|
|
8193
|
-
next(new
|
|
8417
|
+
next(new BadRequestError41(error.message));
|
|
8194
8418
|
return;
|
|
8195
8419
|
}
|
|
8196
8420
|
try {
|
|
@@ -8221,16 +8445,16 @@ function usePSGCController() {
|
|
|
8221
8445
|
const prefix = req.query.prefix ? String(req.query.prefix) : "";
|
|
8222
8446
|
const isPageNumber = isFinite(page);
|
|
8223
8447
|
if (!isPageNumber) {
|
|
8224
|
-
next(new
|
|
8448
|
+
next(new BadRequestError41("Invalid page number."));
|
|
8225
8449
|
return;
|
|
8226
8450
|
}
|
|
8227
8451
|
const isLimitNumber = isFinite(limit);
|
|
8228
8452
|
if (!isLimitNumber) {
|
|
8229
|
-
next(new
|
|
8453
|
+
next(new BadRequestError41("Invalid limit number."));
|
|
8230
8454
|
return;
|
|
8231
8455
|
}
|
|
8232
8456
|
if (error) {
|
|
8233
|
-
next(new
|
|
8457
|
+
next(new BadRequestError41(error.message));
|
|
8234
8458
|
return;
|
|
8235
8459
|
}
|
|
8236
8460
|
try {
|
|
@@ -8254,7 +8478,7 @@ function usePSGCController() {
|
|
|
8254
8478
|
});
|
|
8255
8479
|
const { error } = validation.validate({ id });
|
|
8256
8480
|
if (error) {
|
|
8257
|
-
next(new
|
|
8481
|
+
next(new BadRequestError41(error.message));
|
|
8258
8482
|
return;
|
|
8259
8483
|
}
|
|
8260
8484
|
try {
|
|
@@ -8275,7 +8499,7 @@ function usePSGCController() {
|
|
|
8275
8499
|
});
|
|
8276
8500
|
const { error } = validation.validate({ name });
|
|
8277
8501
|
if (error) {
|
|
8278
|
-
next(new
|
|
8502
|
+
next(new BadRequestError41(error.message));
|
|
8279
8503
|
return;
|
|
8280
8504
|
}
|
|
8281
8505
|
try {
|
|
@@ -8299,7 +8523,7 @@ function usePSGCController() {
|
|
|
8299
8523
|
});
|
|
8300
8524
|
const { error } = validation.validate({ _id, field, value });
|
|
8301
8525
|
if (error) {
|
|
8302
|
-
next(new
|
|
8526
|
+
next(new BadRequestError41(error.message));
|
|
8303
8527
|
return;
|
|
8304
8528
|
}
|
|
8305
8529
|
try {
|
|
@@ -8317,7 +8541,7 @@ function usePSGCController() {
|
|
|
8317
8541
|
});
|
|
8318
8542
|
const { error } = validation.validate({ _id });
|
|
8319
8543
|
if (error) {
|
|
8320
|
-
next(new
|
|
8544
|
+
next(new BadRequestError41(error.message));
|
|
8321
8545
|
return;
|
|
8322
8546
|
}
|
|
8323
8547
|
try {
|
|
@@ -8360,7 +8584,6 @@ export {
|
|
|
8360
8584
|
MONGO_DB,
|
|
8361
8585
|
MONGO_URI,
|
|
8362
8586
|
MOrg,
|
|
8363
|
-
MRole,
|
|
8364
8587
|
MToken,
|
|
8365
8588
|
MUser,
|
|
8366
8589
|
MUserRole,
|
|
@@ -8390,6 +8613,7 @@ export {
|
|
|
8390
8613
|
modelPSGC,
|
|
8391
8614
|
modelPermission,
|
|
8392
8615
|
modelPermissionGroup,
|
|
8616
|
+
modelRole,
|
|
8393
8617
|
schemaApp,
|
|
8394
8618
|
schemaAppUpdate,
|
|
8395
8619
|
schemaOrg,
|
|
@@ -8398,6 +8622,8 @@ export {
|
|
|
8398
8622
|
schemaPermissionGroup,
|
|
8399
8623
|
schemaPermissionGroupUpdate,
|
|
8400
8624
|
schemaPermissionUpdate,
|
|
8625
|
+
schemaRole,
|
|
8626
|
+
schemaRoleUpdate,
|
|
8401
8627
|
transactionSchema,
|
|
8402
8628
|
useAddressController,
|
|
8403
8629
|
useAddressRepo,
|
|
@@ -8429,6 +8655,7 @@ export {
|
|
|
8429
8655
|
usePermissionService,
|
|
8430
8656
|
useRoleController,
|
|
8431
8657
|
useRoleRepo,
|
|
8658
|
+
useRoleService,
|
|
8432
8659
|
useTokenRepo,
|
|
8433
8660
|
useTranscribeService,
|
|
8434
8661
|
useUserController,
|