@eeplatform/core 1.7.4 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @eeplatform/core
2
2
 
3
+ ## 1.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - de639da: PSGC mgmt - initial release
8
+
3
9
  ## 1.7.4
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -874,6 +874,48 @@ declare function useUtilController(): {
874
874
 
875
875
  declare const transactionSchema: Joi.ObjectSchema<any>;
876
876
 
877
+ type TPSGC = {
878
+ _id?: ObjectId;
879
+ code: number;
880
+ name: string;
881
+ type: string;
882
+ };
883
+ declare const schemaPSGC: Joi.ObjectSchema<any>;
884
+ declare function modelPSGC(data: any): {
885
+ code: any;
886
+ name: any;
887
+ type: any;
888
+ };
889
+
890
+ declare function usePSGCRepo(): {
891
+ createIndexes: () => Promise<void>;
892
+ add: (value: TPSGC, session?: ClientSession) => Promise<ObjectId>;
893
+ getAll: ({ search, page, limit, sort, status, }?: {
894
+ search?: string | undefined;
895
+ page?: number | undefined;
896
+ limit?: number | undefined;
897
+ sort?: {} | undefined;
898
+ status?: string | undefined;
899
+ }) => Promise<Record<string, any> | {
900
+ items: any[];
901
+ pages: number;
902
+ pageRange: string;
903
+ }>;
904
+ getById: (_id: string | ObjectId) => Promise<TPSGC>;
905
+ updateFieldById: ({ _id, field, value }?: {
906
+ _id: string | ObjectId;
907
+ field: string;
908
+ value: string;
909
+ }, session?: ClientSession) => Promise<string>;
910
+ deleteById: (_id: string | ObjectId) => Promise<string>;
911
+ getByName: ({ name, cityMunicipality, code, type }?: {
912
+ name: string;
913
+ cityMunicipality?: boolean;
914
+ code?: number;
915
+ type?: string;
916
+ }) => Promise<TPSGC | null>;
917
+ };
918
+
877
919
  declare const MONGO_URI: string;
878
920
  declare const MONGO_DB: string;
879
921
  declare const PORT: number;
@@ -913,4 +955,4 @@ declare const GEMINI_API_KEY: string;
913
955
  declare const ASSEMBLY_AI_API_KEY: string;
914
956
  declare const DOMAIN: string;
915
957
 
916
- export { ACCESS_TOKEN_EXPIRY, ACCESS_TOKEN_SECRET, APP_ACCOUNT, APP_MAIN, ASSEMBLY_AI_API_KEY, AudioFileData, AudioTranscriptionOptions, AudioTranscriptionResult, DEFAULT_USER_EMAIL, DEFAULT_USER_FIRST_NAME, DEFAULT_USER_LAST_NAME, DEFAULT_USER_PASSWORD, DOMAIN, GEMINI_API_KEY, MAILER_EMAIL, MAILER_PASSWORD, MAILER_TRANSPORT_HOST, MAILER_TRANSPORT_PORT, MAILER_TRANSPORT_SECURE, MAddress, MBuilding, MBuildingUnit, MFile, MMember, MONGO_DB, MONGO_URI, MOrg, MRole, MToken, MUser, MUserRole, MVerification, PAYPAL_API_URL, PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PORT, PhonemeMatchOptions, PhonemeMatchResult, REDIS_HOST, REDIS_PASSWORD, REDIS_PORT, REFRESH_TOKEN_EXPIRY, REFRESH_TOKEN_SECRET, SECRET_KEY, SPACES_ACCESS_KEY, SPACES_BUCKET, SPACES_ENDPOINT, SPACES_REGION, SPACES_SECRET_KEY, TAddress, TBuilding, TBuildingUnit, TCounter, TFile, TMember, TMiniRole, TOrg, TRole, TToken, TUser, TUserRole, TVerification, TVerificationMetadata, VERIFICATION_FORGET_PASSWORD_DURATION, VERIFICATION_USER_INVITE_DURATION, XENDIT_BASE_URL, XENDIT_SECRET_KEY, addressSchema, isDev, schemaBuilding, schemaBuildingUnit, schemaOrg, schemaUpdateOptions, transactionSchema, useAddressController, useAddressRepo, useAudioTranscriptionController, useAuthController, useAuthService, useBuildingController, useBuildingRepo, useBuildingService, useBuildingUnitController, useBuildingUnitRepo, useBuildingUnitService, useCounterModel, useCounterRepo, useFileController, useFileRepo, useFileService, useGeminiAiService, useGitHubService, useMemberController, useMemberRepo, useOrgController, useOrgRepo, useOrgService, useRoleController, useRoleRepo, useTokenRepo, useTranscribeService, useUserController, useUserRepo, useUserService, useUtilController, useVerificationController, useVerificationRepo, useVerificationService };
958
+ export { ACCESS_TOKEN_EXPIRY, ACCESS_TOKEN_SECRET, APP_ACCOUNT, APP_MAIN, ASSEMBLY_AI_API_KEY, AudioFileData, AudioTranscriptionOptions, AudioTranscriptionResult, DEFAULT_USER_EMAIL, DEFAULT_USER_FIRST_NAME, DEFAULT_USER_LAST_NAME, DEFAULT_USER_PASSWORD, DOMAIN, GEMINI_API_KEY, MAILER_EMAIL, MAILER_PASSWORD, MAILER_TRANSPORT_HOST, MAILER_TRANSPORT_PORT, MAILER_TRANSPORT_SECURE, MAddress, MBuilding, MBuildingUnit, MFile, MMember, MONGO_DB, MONGO_URI, MOrg, MRole, MToken, MUser, MUserRole, MVerification, PAYPAL_API_URL, PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PORT, PhonemeMatchOptions, PhonemeMatchResult, REDIS_HOST, REDIS_PASSWORD, REDIS_PORT, REFRESH_TOKEN_EXPIRY, REFRESH_TOKEN_SECRET, SECRET_KEY, SPACES_ACCESS_KEY, SPACES_BUCKET, SPACES_ENDPOINT, SPACES_REGION, SPACES_SECRET_KEY, TAddress, TBuilding, TBuildingUnit, TCounter, TFile, TMember, TMiniRole, TOrg, TPSGC, TRole, TToken, TUser, TUserRole, TVerification, TVerificationMetadata, VERIFICATION_FORGET_PASSWORD_DURATION, VERIFICATION_USER_INVITE_DURATION, XENDIT_BASE_URL, XENDIT_SECRET_KEY, addressSchema, isDev, modelPSGC, schemaBuilding, schemaBuildingUnit, schemaOrg, schemaPSGC, schemaUpdateOptions, transactionSchema, useAddressController, useAddressRepo, useAudioTranscriptionController, useAuthController, useAuthService, useBuildingController, useBuildingRepo, useBuildingService, useBuildingUnitController, useBuildingUnitRepo, useBuildingUnitService, useCounterModel, useCounterRepo, useFileController, useFileRepo, useFileService, useGeminiAiService, useGitHubService, useMemberController, useMemberRepo, useOrgController, useOrgRepo, useOrgService, usePSGCRepo, useRoleController, useRoleRepo, useTokenRepo, useTranscribeService, useUserController, useUserRepo, useUserService, useUtilController, useVerificationController, useVerificationRepo, useVerificationService };
package/dist/index.js CHANGED
@@ -80,9 +80,11 @@ __export(src_exports, {
80
80
  XENDIT_SECRET_KEY: () => XENDIT_SECRET_KEY,
81
81
  addressSchema: () => addressSchema,
82
82
  isDev: () => isDev,
83
+ modelPSGC: () => modelPSGC,
83
84
  schemaBuilding: () => schemaBuilding,
84
85
  schemaBuildingUnit: () => schemaBuildingUnit,
85
86
  schemaOrg: () => schemaOrg,
87
+ schemaPSGC: () => schemaPSGC,
86
88
  schemaUpdateOptions: () => schemaUpdateOptions,
87
89
  transactionSchema: () => transactionSchema,
88
90
  useAddressController: () => useAddressController,
@@ -108,6 +110,7 @@ __export(src_exports, {
108
110
  useOrgController: () => useOrgController,
109
111
  useOrgRepo: () => useOrgRepo,
110
112
  useOrgService: () => useOrgService,
113
+ usePSGCRepo: () => usePSGCRepo,
111
114
  useRoleController: () => useRoleController,
112
115
  useRoleRepo: () => useRoleRepo,
113
116
  useTokenRepo: () => useTokenRepo,
@@ -7022,6 +7025,288 @@ var transactionSchema = import_joi17.default.object({
7022
7025
  updatedAt: import_joi17.default.string().optional().allow("", null),
7023
7026
  deletedAt: import_joi17.default.string().optional().allow("", null)
7024
7027
  });
7028
+
7029
+ // src/resources/phillipine-std-geo-code/psgc.model.ts
7030
+ var import_joi18 = __toESM(require("joi"));
7031
+ var schemaPSGC = import_joi18.default.object({
7032
+ code: import_joi18.default.number().required(),
7033
+ name: import_joi18.default.string().required(),
7034
+ type: import_joi18.default.string().valid("Reg", "Prov", "City", "Mun", "Bgy").required()
7035
+ });
7036
+ function modelPSGC(data) {
7037
+ const { error } = schemaPSGC.validate(data);
7038
+ if (error) {
7039
+ throw new Error(`Invalid PSGC data: ${error.message}`);
7040
+ }
7041
+ return {
7042
+ code: data.code,
7043
+ name: data.name,
7044
+ type: data.type
7045
+ };
7046
+ }
7047
+
7048
+ // src/resources/phillipine-std-geo-code/psgc.repository.ts
7049
+ var import_nodejs_utils39 = require("@eeplatform/nodejs-utils");
7050
+ var import_mongodb21 = require("mongodb");
7051
+ function usePSGCRepo() {
7052
+ const db = import_nodejs_utils39.useAtlas.getDb();
7053
+ if (!db) {
7054
+ throw new Error("Unable to connect to server.");
7055
+ }
7056
+ const namespace_collection = "philippine.standard.geographic.codes";
7057
+ const collection = db.collection(namespace_collection);
7058
+ const { getCache, setCache, delNamespace } = (0, import_nodejs_utils39.useCache)(namespace_collection);
7059
+ async function createIndexes() {
7060
+ try {
7061
+ await collection.createIndexes([
7062
+ { key: { name: 1 } },
7063
+ { key: { name: "text" } },
7064
+ { key: { code: 1 }, unique: true, name: "unique_code" }
7065
+ ]);
7066
+ } catch (error) {
7067
+ throw new Error("Failed to create index on PSGC.");
7068
+ }
7069
+ }
7070
+ function delCachedData() {
7071
+ delNamespace().then(() => {
7072
+ import_nodejs_utils39.logger.log({
7073
+ level: "info",
7074
+ message: `Cache namespace cleared for ${namespace_collection}`
7075
+ });
7076
+ }).catch((err) => {
7077
+ import_nodejs_utils39.logger.log({
7078
+ level: "error",
7079
+ message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
7080
+ });
7081
+ });
7082
+ }
7083
+ async function add(value, session) {
7084
+ try {
7085
+ value = modelPSGC(value);
7086
+ const res = await collection.insertOne(value, { session });
7087
+ delCachedData();
7088
+ return res.insertedId;
7089
+ } catch (error) {
7090
+ import_nodejs_utils39.logger.log({
7091
+ level: "error",
7092
+ message: error.message
7093
+ });
7094
+ if (error instanceof import_nodejs_utils39.AppError) {
7095
+ throw error;
7096
+ } else {
7097
+ const isDuplicated = error.message.includes("duplicate");
7098
+ if (isDuplicated) {
7099
+ throw new import_nodejs_utils39.BadRequestError("Region already exists.");
7100
+ }
7101
+ throw new Error("Failed to create PSGC.");
7102
+ }
7103
+ }
7104
+ }
7105
+ async function getAll({
7106
+ search = "",
7107
+ page = 1,
7108
+ limit = 10,
7109
+ sort = {},
7110
+ status = "active"
7111
+ } = {}) {
7112
+ page = page > 0 ? page - 1 : 0;
7113
+ const query = {
7114
+ deletedAt: { $in: ["", null] },
7115
+ status
7116
+ };
7117
+ sort = Object.keys(sort).length > 0 ? sort : { _id: 1 };
7118
+ const cacheKeyOptions = {
7119
+ status,
7120
+ page,
7121
+ limit,
7122
+ sort: JSON.stringify(sort)
7123
+ };
7124
+ if (search) {
7125
+ query.$text = { $search: search };
7126
+ cacheKeyOptions.search = search;
7127
+ }
7128
+ const cacheKey = (0, import_nodejs_utils39.makeCacheKey)(namespace_collection, cacheKeyOptions);
7129
+ import_nodejs_utils39.logger.log({
7130
+ level: "info",
7131
+ message: `Cache key for getAll PSGC: ${cacheKey}`
7132
+ });
7133
+ try {
7134
+ const cached = await getCache(cacheKey);
7135
+ if (cached) {
7136
+ import_nodejs_utils39.logger.log({
7137
+ level: "info",
7138
+ message: `Cache hit for getAll PSGC: ${cacheKey}`
7139
+ });
7140
+ return cached;
7141
+ }
7142
+ const items = await collection.aggregate([
7143
+ { $match: query },
7144
+ { $sort: sort },
7145
+ { $skip: page * limit },
7146
+ { $limit: limit }
7147
+ ]).toArray();
7148
+ const length = await collection.countDocuments(query);
7149
+ const data = (0, import_nodejs_utils39.paginate)(items, page, limit, length);
7150
+ setCache(cacheKey, data, 600).then(() => {
7151
+ import_nodejs_utils39.logger.log({
7152
+ level: "info",
7153
+ message: `Cache set for getAll PSGC: ${cacheKey}`
7154
+ });
7155
+ }).catch((err) => {
7156
+ import_nodejs_utils39.logger.log({
7157
+ level: "error",
7158
+ message: `Failed to set cache for getAll PSGC: ${err.message}`
7159
+ });
7160
+ });
7161
+ return data;
7162
+ } catch (error) {
7163
+ import_nodejs_utils39.logger.log({ level: "error", message: `${error}` });
7164
+ throw error;
7165
+ }
7166
+ }
7167
+ async function getById(_id) {
7168
+ try {
7169
+ _id = new import_mongodb21.ObjectId(_id);
7170
+ } catch (error) {
7171
+ throw new import_nodejs_utils39.BadRequestError("Invalid ID.");
7172
+ }
7173
+ const cacheKey = (0, import_nodejs_utils39.makeCacheKey)(namespace_collection, { _id: String(_id) });
7174
+ try {
7175
+ const cached = await getCache(cacheKey);
7176
+ if (cached) {
7177
+ import_nodejs_utils39.logger.log({
7178
+ level: "info",
7179
+ message: `Cache hit for getById PSGC: ${cacheKey}`
7180
+ });
7181
+ return cached;
7182
+ }
7183
+ const result = await collection.findOne({
7184
+ _id,
7185
+ deletedAt: { $in: ["", null] }
7186
+ });
7187
+ if (!result) {
7188
+ throw new import_nodejs_utils39.BadRequestError("Region not found.");
7189
+ }
7190
+ setCache(cacheKey, result, 300).then(() => {
7191
+ import_nodejs_utils39.logger.log({
7192
+ level: "info",
7193
+ message: `Cache set for PSGC by id: ${cacheKey}`
7194
+ });
7195
+ }).catch((err) => {
7196
+ import_nodejs_utils39.logger.log({
7197
+ level: "error",
7198
+ message: `Failed to set cache for PSGC by id: ${err.message}`
7199
+ });
7200
+ });
7201
+ return result;
7202
+ } catch (error) {
7203
+ if (error instanceof import_nodejs_utils39.AppError) {
7204
+ throw error;
7205
+ } else {
7206
+ throw new import_nodejs_utils39.InternalServerError("Failed to get PSGC.");
7207
+ }
7208
+ }
7209
+ }
7210
+ async function getByName({ name, cityMunicipality = false, code = 0, type = "" } = {}) {
7211
+ const query = { $text: { $search: name } };
7212
+ const cacheKeyOptions = { name };
7213
+ if (cityMunicipality) {
7214
+ query.type = { $in: ["City", "Mun"] };
7215
+ cacheKeyOptions.type = cityMunicipality;
7216
+ }
7217
+ if (type && !cityMunicipality) {
7218
+ query.type = type;
7219
+ cacheKeyOptions.type = type;
7220
+ }
7221
+ if (code) {
7222
+ query.code = { $gt: code };
7223
+ if (cityMunicipality) {
7224
+ query.code.$lt = code + 1e6;
7225
+ }
7226
+ cacheKeyOptions.code = code;
7227
+ }
7228
+ const cacheKey = (0, import_nodejs_utils39.makeCacheKey)(namespace_collection, { name });
7229
+ try {
7230
+ const cached = await getCache(cacheKey);
7231
+ if (cached) {
7232
+ import_nodejs_utils39.logger.log({
7233
+ level: "info",
7234
+ message: `Cache hit for getByName PSGC: ${cacheKey}`
7235
+ });
7236
+ return cached;
7237
+ }
7238
+ const result = await collection.findOne(query);
7239
+ setCache(cacheKey, result, 300).then(() => {
7240
+ import_nodejs_utils39.logger.log({
7241
+ level: "info",
7242
+ message: `Cache set for PSGC by name: ${cacheKey}`
7243
+ });
7244
+ }).catch((err) => {
7245
+ import_nodejs_utils39.logger.log({
7246
+ level: "error",
7247
+ message: `Failed to set cache for PSGC by name: ${err.message}`
7248
+ });
7249
+ });
7250
+ return result;
7251
+ } catch (error) {
7252
+ if (error instanceof import_nodejs_utils39.AppError) {
7253
+ throw error;
7254
+ } else {
7255
+ throw new import_nodejs_utils39.InternalServerError("Failed to get PSGC.");
7256
+ }
7257
+ }
7258
+ }
7259
+ async function updateFieldById({ _id, field, value } = {}, session) {
7260
+ const allowedFields = ["name"];
7261
+ if (!allowedFields.includes(field)) {
7262
+ throw new import_nodejs_utils39.BadRequestError(
7263
+ `Field "${field}" is not allowed to be updated.`
7264
+ );
7265
+ }
7266
+ try {
7267
+ _id = new import_mongodb21.ObjectId(_id);
7268
+ } catch (error) {
7269
+ throw new import_nodejs_utils39.BadRequestError("Invalid ID.");
7270
+ }
7271
+ try {
7272
+ await collection.updateOne(
7273
+ { _id, deletedAt: { $in: ["", null] } },
7274
+ { $set: { [field]: value, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
7275
+ { session }
7276
+ );
7277
+ delCachedData();
7278
+ return `Successfully updated PSGC ${field}.`;
7279
+ } catch (error) {
7280
+ throw new import_nodejs_utils39.InternalServerError(`Failed to update PSGC ${field}.`);
7281
+ }
7282
+ }
7283
+ async function deleteById(_id) {
7284
+ try {
7285
+ _id = new import_mongodb21.ObjectId(_id);
7286
+ } catch (error) {
7287
+ throw new import_nodejs_utils39.BadRequestError("Invalid ID.");
7288
+ }
7289
+ try {
7290
+ await collection.updateOne(
7291
+ { _id },
7292
+ { $set: { status: "deleted", deletedAt: (/* @__PURE__ */ new Date()).toISOString() } }
7293
+ );
7294
+ delCachedData();
7295
+ return "Successfully deleted PSGC.";
7296
+ } catch (error) {
7297
+ throw new import_nodejs_utils39.InternalServerError("Failed to delete PSGC.");
7298
+ }
7299
+ }
7300
+ return {
7301
+ createIndexes,
7302
+ add,
7303
+ getAll,
7304
+ getById,
7305
+ updateFieldById,
7306
+ deleteById,
7307
+ getByName
7308
+ };
7309
+ }
7025
7310
  // Annotate the CommonJS export names for ESM import in node:
7026
7311
  0 && (module.exports = {
7027
7312
  ACCESS_TOKEN_EXPIRY,
@@ -7074,9 +7359,11 @@ var transactionSchema = import_joi17.default.object({
7074
7359
  XENDIT_SECRET_KEY,
7075
7360
  addressSchema,
7076
7361
  isDev,
7362
+ modelPSGC,
7077
7363
  schemaBuilding,
7078
7364
  schemaBuildingUnit,
7079
7365
  schemaOrg,
7366
+ schemaPSGC,
7080
7367
  schemaUpdateOptions,
7081
7368
  transactionSchema,
7082
7369
  useAddressController,
@@ -7102,6 +7389,7 @@ var transactionSchema = import_joi17.default.object({
7102
7389
  useOrgController,
7103
7390
  useOrgRepo,
7104
7391
  useOrgService,
7392
+ usePSGCRepo,
7105
7393
  useRoleController,
7106
7394
  useRoleRepo,
7107
7395
  useTokenRepo,