@eeplatform/core 1.8.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -7040,10 +7040,10 @@ var transactionSchema = Joi17.object({
7040
7040
  deletedAt: Joi17.string().optional().allow("", null)
7041
7041
  });
7042
7042
 
7043
- // src/resources/phillipine-std-geo-code/psgc.model.ts
7043
+ // src/resources/psgc/psgc.model.ts
7044
7044
  import Joi18 from "joi";
7045
7045
  var schemaPSGC = Joi18.object({
7046
- code: Joi18.number().required(),
7046
+ code: Joi18.string().length(10).required(),
7047
7047
  name: Joi18.string().required(),
7048
7048
  type: Joi18.string().valid("Reg", "Prov", "City", "Mun", "Bgy").required()
7049
7049
  });
@@ -7059,7 +7059,7 @@ function modelPSGC(data) {
7059
7059
  };
7060
7060
  }
7061
7061
 
7062
- // src/resources/phillipine-std-geo-code/psgc.repository.ts
7062
+ // src/resources/psgc/psgc.repository.ts
7063
7063
  import {
7064
7064
  AppError as AppError14,
7065
7065
  BadRequestError as BadRequestError34,
@@ -7076,7 +7076,7 @@ function usePSGCRepo() {
7076
7076
  if (!db) {
7077
7077
  throw new Error("Unable to connect to server.");
7078
7078
  }
7079
- const namespace_collection = "philippine.standard.geographic.codes";
7079
+ const namespace_collection = "psgc";
7080
7080
  const collection = db.collection(namespace_collection);
7081
7081
  const { getCache, setCache, delNamespace } = useCache14(namespace_collection);
7082
7082
  async function createIndexes() {
@@ -7130,20 +7130,27 @@ function usePSGCRepo() {
7130
7130
  page = 1,
7131
7131
  limit = 10,
7132
7132
  sort = {},
7133
- status = "active"
7133
+ type = "",
7134
+ prefix = ""
7134
7135
  } = {}) {
7135
7136
  page = page > 0 ? page - 1 : 0;
7136
7137
  const query = {
7137
- deletedAt: { $in: ["", null] },
7138
- status
7138
+ type
7139
7139
  };
7140
- sort = Object.keys(sort).length > 0 ? sort : { _id: 1 };
7141
7140
  const cacheKeyOptions = {
7142
- status,
7141
+ type,
7143
7142
  page,
7144
7143
  limit,
7145
7144
  sort: JSON.stringify(sort)
7146
7145
  };
7146
+ if (prefix) {
7147
+ query.code = { $regex: `^${prefix}` };
7148
+ cacheKeyOptions.prefix = prefix;
7149
+ }
7150
+ if (type === "City" || type === "Mun") {
7151
+ query.type = { $in: ["City", "Mun"] };
7152
+ }
7153
+ sort = Object.keys(sort).length > 0 ? sort : { _id: 1 };
7147
7154
  if (search) {
7148
7155
  query.$text = { $search: search };
7149
7156
  cacheKeyOptions.search = search;
@@ -7230,23 +7237,16 @@ function usePSGCRepo() {
7230
7237
  }
7231
7238
  }
7232
7239
  }
7233
- async function getByName({ name, cityMunicipality = false, code = 0, type = "" } = {}) {
7240
+ async function getByName({ name, prefix = "", type = "" } = {}) {
7234
7241
  const query = { $text: { $search: name } };
7235
7242
  const cacheKeyOptions = { name };
7236
- if (cityMunicipality) {
7243
+ if (["City", "Mun"].includes(type)) {
7237
7244
  query.type = { $in: ["City", "Mun"] };
7238
- cacheKeyOptions.type = cityMunicipality;
7239
- }
7240
- if (type && !cityMunicipality) {
7241
- query.type = type;
7242
7245
  cacheKeyOptions.type = type;
7243
7246
  }
7244
- if (code) {
7245
- query.code = { $gt: code };
7246
- if (cityMunicipality) {
7247
- query.code.$lt = code + 1e6;
7248
- }
7249
- cacheKeyOptions.code = code;
7247
+ if (prefix) {
7248
+ query.code = { $regex: `^${prefix}` };
7249
+ cacheKeyOptions.prefix = prefix;
7250
7250
  }
7251
7251
  const cacheKey = makeCacheKey13(namespace_collection, { name });
7252
7252
  try {
@@ -7330,6 +7330,170 @@ function usePSGCRepo() {
7330
7330
  getByName
7331
7331
  };
7332
7332
  }
7333
+
7334
+ // src/resources/psgc/psgc.controller.ts
7335
+ import { BadRequestError as BadRequestError35 } from "@eeplatform/nodejs-utils";
7336
+ import Joi19 from "joi";
7337
+ function usePSGCController() {
7338
+ const {
7339
+ add: _add,
7340
+ getAll: _getAll,
7341
+ getById: _getById,
7342
+ getByName: _getByName,
7343
+ updateFieldById: _updateFieldById,
7344
+ deleteById: _deleteById
7345
+ } = usePSGCRepo();
7346
+ async function add(req, res, next) {
7347
+ const value = req.body;
7348
+ const { error } = schemaPSGC.validate(value);
7349
+ if (error) {
7350
+ next(new BadRequestError35(error.message));
7351
+ return;
7352
+ }
7353
+ try {
7354
+ const data = await _add(value);
7355
+ res.json({
7356
+ message: "Successfully created region.",
7357
+ data
7358
+ });
7359
+ return;
7360
+ } catch (error2) {
7361
+ next(error2);
7362
+ }
7363
+ }
7364
+ async function getAll(req, res, next) {
7365
+ const query = req.query;
7366
+ const validation = Joi19.object({
7367
+ page: Joi19.number().min(1).optional().allow("", null),
7368
+ limit: Joi19.number().min(1).optional().allow("", null),
7369
+ search: Joi19.string().optional().allow("", null),
7370
+ type: Joi19.string().valid("Reg", "Prov", "City", "Mun", "Bgy").required(),
7371
+ prefix: Joi19.string().optional().allow("", null)
7372
+ });
7373
+ const { error } = validation.validate(query);
7374
+ const page = typeof req.query.page === "string" ? Number(req.query.page) : 1;
7375
+ const limit = typeof req.query.limit === "string" ? Number(req.query.limit) : 10;
7376
+ const search = req.query.search ?? "";
7377
+ const type = req.query.type ?? "";
7378
+ const prefix = req.query.prefix ? String(req.query.prefix) : "";
7379
+ const isPageNumber = isFinite(page);
7380
+ if (!isPageNumber) {
7381
+ next(new BadRequestError35("Invalid page number."));
7382
+ return;
7383
+ }
7384
+ const isLimitNumber = isFinite(limit);
7385
+ if (!isLimitNumber) {
7386
+ next(new BadRequestError35("Invalid limit number."));
7387
+ return;
7388
+ }
7389
+ if (error) {
7390
+ next(new BadRequestError35(error.message));
7391
+ return;
7392
+ }
7393
+ try {
7394
+ const data = await _getAll({
7395
+ page,
7396
+ limit,
7397
+ search,
7398
+ type,
7399
+ prefix
7400
+ });
7401
+ res.json(data);
7402
+ return;
7403
+ } catch (error2) {
7404
+ next(error2);
7405
+ }
7406
+ }
7407
+ async function getById(req, res, next) {
7408
+ const id = req.params.id;
7409
+ const validation = Joi19.object({
7410
+ id: Joi19.string().hex().required()
7411
+ });
7412
+ const { error } = validation.validate({ id });
7413
+ if (error) {
7414
+ next(new BadRequestError35(error.message));
7415
+ return;
7416
+ }
7417
+ try {
7418
+ const data = await _getById(id);
7419
+ res.json({
7420
+ message: "Successfully retrieved region.",
7421
+ data
7422
+ });
7423
+ return;
7424
+ } catch (error2) {
7425
+ next(error2);
7426
+ }
7427
+ }
7428
+ async function getByName(req, res, next) {
7429
+ const name = req.params.name;
7430
+ const validation = Joi19.object({
7431
+ name: Joi19.string().required()
7432
+ });
7433
+ const { error } = validation.validate({ name });
7434
+ if (error) {
7435
+ next(new BadRequestError35(error.message));
7436
+ return;
7437
+ }
7438
+ try {
7439
+ const data = await _getByName({ name });
7440
+ res.json({
7441
+ message: "Successfully retrieved region.",
7442
+ data
7443
+ });
7444
+ return;
7445
+ } catch (error2) {
7446
+ next(error2);
7447
+ }
7448
+ }
7449
+ async function updateField(req, res, next) {
7450
+ const _id = req.params.id;
7451
+ const { field, value } = req.body;
7452
+ const validation = Joi19.object({
7453
+ _id: Joi19.string().hex().required(),
7454
+ field: Joi19.string().valid("name", "director", "directorName").required(),
7455
+ value: Joi19.string().required()
7456
+ });
7457
+ const { error } = validation.validate({ _id, field, value });
7458
+ if (error) {
7459
+ next(new BadRequestError35(error.message));
7460
+ return;
7461
+ }
7462
+ try {
7463
+ const message = await _updateFieldById({ _id, field, value });
7464
+ res.json({ message });
7465
+ return;
7466
+ } catch (error2) {
7467
+ next(error2);
7468
+ }
7469
+ }
7470
+ async function deleteById(req, res, next) {
7471
+ const _id = req.params.id;
7472
+ const validation = Joi19.object({
7473
+ _id: Joi19.string().hex().required()
7474
+ });
7475
+ const { error } = validation.validate({ _id });
7476
+ if (error) {
7477
+ next(new BadRequestError35(error.message));
7478
+ return;
7479
+ }
7480
+ try {
7481
+ const message = await _deleteById(_id);
7482
+ res.json({ message });
7483
+ return;
7484
+ } catch (error2) {
7485
+ next(error2);
7486
+ }
7487
+ }
7488
+ return {
7489
+ add,
7490
+ getAll,
7491
+ getById,
7492
+ getByName,
7493
+ updateField,
7494
+ deleteById
7495
+ };
7496
+ }
7333
7497
  export {
7334
7498
  ACCESS_TOKEN_EXPIRY,
7335
7499
  ACCESS_TOKEN_SECRET,
@@ -7411,6 +7575,7 @@ export {
7411
7575
  useOrgController,
7412
7576
  useOrgRepo,
7413
7577
  useOrgService,
7578
+ usePSGCController,
7414
7579
  usePSGCRepo,
7415
7580
  useRoleController,
7416
7581
  useRoleRepo,