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