aaspai-authx 0.1.7 → 0.1.8

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.
@@ -43,6 +43,7 @@ var import_bcryptjs = __toESM(require("bcryptjs"), 1);
43
43
  var import_crypto = require("crypto");
44
44
  var import_express = __toESM(require("express"), 1);
45
45
  var import_jsonwebtoken4 = __toESM(require("jsonwebtoken"), 1);
46
+ var import_mongoose5 = __toESM(require("mongoose"), 1);
46
47
 
47
48
  // src/config/loadConfig.ts
48
49
  function loadConfig() {
@@ -243,7 +244,7 @@ var MetadataSchema = new import_mongoose2.default.Schema(
243
244
  );
244
245
  var OrgUserSchema = new import_mongoose2.default.Schema(
245
246
  {
246
- id: { type: String, default: (0, import_uuid.v4)(), index: true, unique: true },
247
+ id: { type: String, default: import_uuid.v4, index: true, unique: true },
247
248
  email: { type: String, required: true, unique: true },
248
249
  firstName: { type: String, required: true },
249
250
  lastName: { type: String, required: true },
@@ -258,6 +259,11 @@ var OrgUserSchema = new import_mongoose2.default.Schema(
258
259
  },
259
260
  { timestamps: true, collection: "users" }
260
261
  );
262
+ OrgUserSchema.index({ projectId: 1, createdAt: -1 });
263
+ OrgUserSchema.index({ projectId: 1, updatedAt: -1 });
264
+ OrgUserSchema.index({ projectId: 1, email: 1 });
265
+ OrgUserSchema.index({ projectId: 1, emailVerified: 1, createdAt: -1 });
266
+ OrgUserSchema.index({ projectId: 1, firstName: 1, lastName: 1 });
261
267
  var OrgUser = import_mongoose2.default.model("OrgUser", OrgUserSchema);
262
268
 
263
269
  // src/utils/extract.ts
@@ -1666,6 +1672,212 @@ function createAuthRouter(options = {}) {
1666
1672
  const user = await OrgUser.find({ projectId: req.query.projectId }).lean();
1667
1673
  res.json(user || null);
1668
1674
  });
1675
+ r.get("/get-users-by-pagination", async (req, res) => {
1676
+ try {
1677
+ const projectId = String(req.query.projectId || "");
1678
+ if (!projectId) {
1679
+ return res.status(400).json({
1680
+ ok: false,
1681
+ error: "projectId is required"
1682
+ });
1683
+ }
1684
+ const page = Math.max(
1685
+ 1,
1686
+ parseInt(String(req.query.page || "1"), 10) || 1
1687
+ );
1688
+ const limit = Math.min(
1689
+ 200,
1690
+ Math.max(1, parseInt(String(req.query.limit || "20"), 10) || 20)
1691
+ );
1692
+ const skip = (page - 1) * limit;
1693
+ const allowedSortFields = [
1694
+ "createdAt",
1695
+ "updatedAt",
1696
+ "email",
1697
+ "firstName",
1698
+ "lastName"
1699
+ ];
1700
+ const sortBy = allowedSortFields.includes(String(req.query.sortBy || "")) ? String(req.query.sortBy) : "createdAt";
1701
+ const sortOrder = String(req.query.sortOrder || "desc").toLowerCase() === "asc" ? 1 : -1;
1702
+ const sortObj = { [sortBy]: sortOrder };
1703
+ const allowedDateFields = ["createdAt", "updatedAt"];
1704
+ const dateField = allowedDateFields.includes(
1705
+ String(req.query.dateField || "")
1706
+ ) ? String(req.query.dateField) : "createdAt";
1707
+ const baseQuery = { projectId };
1708
+ if (req.query.emailVerified !== void 0) {
1709
+ const emailVerified = String(req.query.emailVerified).toLowerCase();
1710
+ if (emailVerified === "true" || emailVerified === "false") {
1711
+ baseQuery.emailVerified = emailVerified === "true";
1712
+ }
1713
+ }
1714
+ if (req.query.from || req.query.to) {
1715
+ const dateFilter = {};
1716
+ if (req.query.from) {
1717
+ const fromDate = new Date(String(req.query.from));
1718
+ if (!isNaN(fromDate.getTime())) {
1719
+ dateFilter.$gte = fromDate;
1720
+ }
1721
+ }
1722
+ if (req.query.to) {
1723
+ const toDate = new Date(String(req.query.to));
1724
+ if (!isNaN(toDate.getTime())) {
1725
+ dateFilter.$lte = toDate;
1726
+ }
1727
+ }
1728
+ if (Object.keys(dateFilter).length > 0) {
1729
+ baseQuery[dateField] = dateFilter;
1730
+ }
1731
+ }
1732
+ if (req.query.email) {
1733
+ const emailSearch = escapeRegex(String(req.query.email));
1734
+ baseQuery.email = { $regex: emailSearch, $options: "i" };
1735
+ }
1736
+ const projection = {
1737
+ id: 1,
1738
+ email: 1,
1739
+ firstName: 1,
1740
+ lastName: 1,
1741
+ orgId: 1,
1742
+ projectId: 1,
1743
+ roles: 1,
1744
+ emailVerified: 1,
1745
+ lastEmailSent: 1,
1746
+ lastPasswordReset: 1,
1747
+ metadata: 1,
1748
+ createdAt: 1,
1749
+ updatedAt: 1
1750
+ };
1751
+ if (req.query.name) {
1752
+ const nameSearch = escapeRegex(String(req.query.name));
1753
+ const nameRegex = { $regex: nameSearch, $options: "i" };
1754
+ const pipeline = [
1755
+ { $match: baseQuery },
1756
+ {
1757
+ $addFields: {
1758
+ fullName: {
1759
+ $concat: [
1760
+ { $ifNull: ["$firstName", ""] },
1761
+ " ",
1762
+ { $ifNull: ["$lastName", ""] }
1763
+ ]
1764
+ }
1765
+ }
1766
+ },
1767
+ {
1768
+ $match: {
1769
+ $or: [
1770
+ { firstName: nameRegex },
1771
+ { lastName: nameRegex },
1772
+ { fullName: nameRegex }
1773
+ ]
1774
+ }
1775
+ },
1776
+ {
1777
+ $facet: {
1778
+ data: [
1779
+ { $sort: sortObj },
1780
+ { $skip: skip },
1781
+ { $limit: limit },
1782
+ { $project: projection }
1783
+ ],
1784
+ total: [{ $count: "count" }]
1785
+ }
1786
+ }
1787
+ ];
1788
+ const result = await OrgUser.aggregate(pipeline);
1789
+ const data2 = result[0]?.data || [];
1790
+ const total2 = result[0]?.total[0]?.count || 0;
1791
+ const totalPages2 = Math.ceil(total2 / limit);
1792
+ return res.json({
1793
+ ok: true,
1794
+ page,
1795
+ limit,
1796
+ total: total2,
1797
+ totalPages: totalPages2,
1798
+ data: data2
1799
+ });
1800
+ }
1801
+ const [data, total] = await Promise.all([
1802
+ OrgUser.find(baseQuery).select(projection).sort(sortObj).skip(skip).limit(limit).lean(),
1803
+ OrgUser.countDocuments(baseQuery)
1804
+ ]);
1805
+ const totalPages = Math.ceil(total / limit);
1806
+ return res.json({
1807
+ ok: true,
1808
+ page,
1809
+ limit,
1810
+ total,
1811
+ totalPages,
1812
+ data
1813
+ });
1814
+ } catch (err) {
1815
+ console.error("Get users by pagination error:", err);
1816
+ return res.status(500).json({
1817
+ ok: false,
1818
+ error: "Internal server error"
1819
+ });
1820
+ }
1821
+ });
1822
+ r.get("/users/:userId", async (req, res) => {
1823
+ try {
1824
+ const userId = String(req.params.userId || "").trim();
1825
+ if (!userId) {
1826
+ return res.status(400).json({
1827
+ ok: false,
1828
+ error: "userId is required"
1829
+ });
1830
+ }
1831
+ const projectId = String(req.query.projectId || "").trim() || process.env.PROJECTID || "";
1832
+ if (!projectId) {
1833
+ return res.status(400).json({
1834
+ ok: false,
1835
+ error: "projectId is required"
1836
+ });
1837
+ }
1838
+ const projection = {
1839
+ id: 1,
1840
+ email: 1,
1841
+ firstName: 1,
1842
+ lastName: 1,
1843
+ orgId: 1,
1844
+ projectId: 1,
1845
+ roles: 1,
1846
+ emailVerified: 1,
1847
+ lastEmailSent: 1,
1848
+ lastPasswordReset: 1,
1849
+ metadata: 1,
1850
+ createdAt: 1,
1851
+ updatedAt: 1
1852
+ };
1853
+ let query = { projectId };
1854
+ if (import_mongoose5.default.Types.ObjectId.isValid(userId)) {
1855
+ query = {
1856
+ projectId,
1857
+ $or: [{ _id: new import_mongoose5.default.Types.ObjectId(userId) }, { id: userId }]
1858
+ };
1859
+ } else {
1860
+ query = { projectId, id: userId };
1861
+ }
1862
+ const user = await OrgUser.findOne(query).select(projection).lean();
1863
+ if (!user) {
1864
+ return res.status(404).json({
1865
+ ok: false,
1866
+ error: "User not found"
1867
+ });
1868
+ }
1869
+ return res.json({
1870
+ ok: true,
1871
+ data: user
1872
+ });
1873
+ } catch (err) {
1874
+ console.error("Get user by id error:", err);
1875
+ return res.status(500).json({
1876
+ ok: false,
1877
+ error: "Internal server error"
1878
+ });
1879
+ }
1880
+ });
1669
1881
  return r;
1670
1882
  }
1671
1883
  function setAuthCookies(res, tokens, cookie) {
@@ -1725,6 +1937,9 @@ async function sendRateLimitedEmail({
1725
1937
  await user.save();
1726
1938
  return { rateLimited: false };
1727
1939
  }
1940
+ function escapeRegex(str) {
1941
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1942
+ }
1728
1943
  function generateTokens(user) {
1729
1944
  const accessPayload = {
1730
1945
  sub: user.id.toString(),
@@ -1842,12 +2057,12 @@ var import_express4 = require("express");
1842
2057
  var import_crypto2 = require("crypto");
1843
2058
 
1844
2059
  // src/models/moduleConnection.model.ts
1845
- var import_mongoose5 = __toESM(require("mongoose"), 1);
1846
- var ModuleItemSchema = new import_mongoose5.default.Schema(
2060
+ var import_mongoose6 = __toESM(require("mongoose"), 1);
2061
+ var ModuleItemSchema = new import_mongoose6.default.Schema(
1847
2062
  { id: { type: String, required: true } },
1848
2063
  { _id: false }
1849
2064
  );
1850
- var ModuleConnectionSchema = new import_mongoose5.default.Schema(
2065
+ var ModuleConnectionSchema = new import_mongoose6.default.Schema(
1851
2066
  {
1852
2067
  projectId: { type: String, required: true, index: true },
1853
2068
  modules: {
@@ -1858,14 +2073,14 @@ var ModuleConnectionSchema = new import_mongoose5.default.Schema(
1858
2073
  },
1859
2074
  { timestamps: true, collection: "module_connection" }
1860
2075
  );
1861
- var ModuleConnection = import_mongoose5.default.model(
2076
+ var ModuleConnection = import_mongoose6.default.model(
1862
2077
  "ModuleConnection",
1863
2078
  ModuleConnectionSchema
1864
2079
  );
1865
2080
 
1866
2081
  // src/models/project.model.ts
1867
- var import_mongoose6 = __toESM(require("mongoose"), 1);
1868
- var ProjectSchema = new import_mongoose6.default.Schema(
2082
+ var import_mongoose7 = __toESM(require("mongoose"), 1);
2083
+ var ProjectSchema = new import_mongoose7.default.Schema(
1869
2084
  {
1870
2085
  _id: { type: String, required: true },
1871
2086
  org_id: { type: String, required: true, index: true },
@@ -1875,7 +2090,7 @@ var ProjectSchema = new import_mongoose6.default.Schema(
1875
2090
  },
1876
2091
  { timestamps: true, collection: "projects" }
1877
2092
  );
1878
- var Project = import_mongoose6.default.model("Project", ProjectSchema);
2093
+ var Project = import_mongoose7.default.model("Project", ProjectSchema);
1879
2094
 
1880
2095
  // src/services/projects.service.ts
1881
2096
  var ProjectsService = class {
@@ -1962,8 +2177,8 @@ function requireRole(...roles) {
1962
2177
  }
1963
2178
 
1964
2179
  // src/models/permissions.model.ts
1965
- var import_mongoose7 = __toESM(require("mongoose"), 1);
1966
- var PermissionsSchema = new import_mongoose7.Schema(
2180
+ var import_mongoose8 = __toESM(require("mongoose"), 1);
2181
+ var PermissionsSchema = new import_mongoose8.Schema(
1967
2182
  {
1968
2183
  id: { type: String, required: true, index: true },
1969
2184
  orgId: { type: String, default: null, index: true },
@@ -1978,7 +2193,7 @@ var PermissionsSchema = new import_mongoose7.Schema(
1978
2193
  }
1979
2194
  );
1980
2195
  PermissionsSchema.index({ orgId: 1, key: 1 }, { unique: true });
1981
- var PermissionsModel = import_mongoose7.default.model(
2196
+ var PermissionsModel = import_mongoose8.default.model(
1982
2197
  "Permissions",
1983
2198
  PermissionsSchema,
1984
2199
  "permissions"