@goweekdays/core 2.7.0 → 2.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/dist/index.mjs CHANGED
@@ -6748,7 +6748,7 @@ import { BadRequestError as BadRequestError35 } from "@goweekdays/utils";
6748
6748
  import Joi29 from "joi";
6749
6749
  import { ObjectId as ObjectId20 } from "mongodb";
6750
6750
  var ledgerBillTypes = ["setup-fee", "subscription"];
6751
- var ledgerBillStatuses = ["pending", "completed", "overdue"];
6751
+ var ledgerBillStatuses = ["pending", "paid", "overdue"];
6752
6752
  var schemaLedgerBill = Joi29.object({
6753
6753
  org: Joi29.string().hex().length(24).required(),
6754
6754
  type: Joi29.string().valid(...ledgerBillTypes).required(),
@@ -6761,6 +6761,10 @@ var schemaLedgerBill = Joi29.object({
6761
6761
  paidAt: Joi29.date().optional().allow("", null),
6762
6762
  dueDate: Joi29.date().optional().allow("", null)
6763
6763
  });
6764
+ var schemaLedgerBillingSummary = Joi29.object({
6765
+ status: Joi29.string().valid(...ledgerBillStatuses).required(),
6766
+ org: Joi29.string().hex().length(24).required()
6767
+ });
6764
6768
  function modelLedgerBill(value) {
6765
6769
  const { error } = schemaLedgerBill.validate(value);
6766
6770
  if (error) {
@@ -6953,12 +6957,59 @@ function useLedgerBillingRepo() {
6953
6957
  throw new InternalServerError19("Failed to get ledger billing.");
6954
6958
  }
6955
6959
  }
6960
+ async function getSummary(status, org) {
6961
+ const { error } = schemaLedgerBillingSummary.validate({ status, org });
6962
+ if (error) {
6963
+ throw new Error(`Invalid ledger billing ID: ${error.message}`);
6964
+ }
6965
+ try {
6966
+ org = new ObjectId21(org);
6967
+ } catch (error2) {
6968
+ throw new BadRequestError36("Invalid ledger billing org.");
6969
+ }
6970
+ try {
6971
+ const cacheKey = makeCacheKey13(namespace_collection, {
6972
+ org: String(org),
6973
+ status,
6974
+ tag: "getSummaryByOrgStatus"
6975
+ });
6976
+ const cachedData = await getCache(cacheKey);
6977
+ if (cachedData) {
6978
+ return cachedData;
6979
+ }
6980
+ const data = await collection.aggregate([
6981
+ { $match: { org, status } },
6982
+ {
6983
+ $group: {
6984
+ _id: "$status",
6985
+ totalAmount: { $sum: "$amount" },
6986
+ count: { $sum: 1 }
6987
+ }
6988
+ }
6989
+ ]).toArray();
6990
+ setCache(cacheKey, data[0]).then(() => {
6991
+ logger19.log({
6992
+ level: "info",
6993
+ message: `Cache set for ledger billing summary: ${cacheKey}`
6994
+ });
6995
+ }).catch((err) => {
6996
+ logger19.log({
6997
+ level: "error",
6998
+ message: `Failed to set cache for ledger billing summary: ${err.message}`
6999
+ });
7000
+ });
7001
+ return data[0];
7002
+ } catch (error2) {
7003
+ throw new InternalServerError19("Failed to get ledger billing summary.");
7004
+ }
7005
+ }
6956
7006
  return {
6957
7007
  delCachedData,
6958
7008
  createIndexes,
6959
7009
  add,
6960
7010
  getAll,
6961
- getById
7011
+ getById,
7012
+ getSummary
6962
7013
  };
6963
7014
  }
6964
7015
 
@@ -6966,7 +7017,11 @@ function useLedgerBillingRepo() {
6966
7017
  import Joi31 from "joi";
6967
7018
  import { BadRequestError as BadRequestError37 } from "@goweekdays/utils";
6968
7019
  function useLedgerBillingController() {
6969
- const { getAll: _getAll, getById: _getById } = useLedgerBillingRepo();
7020
+ const {
7021
+ getAll: _getAll,
7022
+ getById: _getById,
7023
+ getSummary: _getSummary
7024
+ } = useLedgerBillingRepo();
6970
7025
  async function getAll(req, res, next) {
6971
7026
  const query = req.query;
6972
7027
  const validation = Joi31.object({
@@ -7022,9 +7077,27 @@ function useLedgerBillingController() {
7022
7077
  next(error2);
7023
7078
  }
7024
7079
  }
7080
+ async function getSummary(req, res, next) {
7081
+ const params = req.params;
7082
+ const { error } = schemaLedgerBillingSummary.validate(params);
7083
+ const org = req.params.org ?? "";
7084
+ const status = req.params.status ?? "";
7085
+ if (error) {
7086
+ next(new BadRequestError37(error.message));
7087
+ return;
7088
+ }
7089
+ try {
7090
+ const summary = await _getSummary(status, org);
7091
+ res.json(summary);
7092
+ return;
7093
+ } catch (error2) {
7094
+ next(error2);
7095
+ }
7096
+ }
7025
7097
  return {
7026
7098
  getAll,
7027
- getById
7099
+ getById,
7100
+ getSummary
7028
7101
  };
7029
7102
  }
7030
7103
 
@@ -7241,7 +7314,7 @@ function useOrgService() {
7241
7314
  description: "Setup payment during organization creation",
7242
7315
  currency: plan.currency,
7243
7316
  type: "setup-fee",
7244
- status: "completed"
7317
+ status: "paid"
7245
7318
  },
7246
7319
  session
7247
7320
  );
@@ -10985,6 +11058,7 @@ export {
10985
11058
  schemaInviteMember,
10986
11059
  schemaJobPost,
10987
11060
  schemaLedgerBill,
11061
+ schemaLedgerBillingSummary,
10988
11062
  schemaMember,
10989
11063
  schemaMemberRole,
10990
11064
  schemaMemberStatus,