Mahmoud7456 commited on
Commit
4946913
·
verified ·
1 Parent(s): 4795c0f

Upload 8 files

Browse files
Files changed (1) hide show
  1. controller/authController.js +74 -1
controller/authController.js CHANGED
@@ -1,4 +1,4 @@
1
- import mongoose from "mongoose";
2
  import User from "../models/UserModel.js";
3
  import Tenant from "../models/TenantModel.js";
4
  import {
@@ -196,6 +196,79 @@ const releaseCashierSlot = async (tenantId, session) => {
196
  );
197
  };
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  export const checkAdmin = async (req, res) => {
200
  try {
201
  const count = await User.countDocuments({ role: "super_admin" });
 
1
+ import mongoose from "mongoose";
2
  import User from "../models/UserModel.js";
3
  import Tenant from "../models/TenantModel.js";
4
  import {
 
196
  );
197
  };
198
 
199
+ const buildTenantSearchFilter = ({ search, status, packageKey }) => {
200
+ const filter = {};
201
+ if (search) {
202
+ filter.$or = [
203
+ { name: { $regex: search, $options: "i" } },
204
+ { slug: { $regex: search, $options: "i" } },
205
+ { ownerName: { $regex: search, $options: "i" } },
206
+ { ownerPhone: { $regex: search, $options: "i" } },
207
+ { whatsappNumber: { $regex: search, $options: "i" } },
208
+ ];
209
+ }
210
+ if (status) filter.status = status;
211
+ if (packageKey) filter.subscriptionPackage = packageKey;
212
+ return filter;
213
+ };
214
+
215
+ const hydrateTenantRowsForDisplay = async (tenants) => {
216
+ return Promise.all(tenants.map((t) => normalizeTenantSubscriptionState(t)));
217
+ };
218
+
219
+ const buildTenantSummary = (rows, totalVisible, totalAll) => {
220
+ const now = new Date();
221
+ const thirtyDaysAgo = new Date(now.setDate(now.getDate() - 30));
222
+
223
+ let totalCashiers = 0;
224
+ let limitSum = 0;
225
+ let nearLimitTenants = 0;
226
+ let overLimitTenants = 0;
227
+ let overduePayments = 0;
228
+ let newTenantsLast30Days = 0;
229
+ let activeTenants = 0;
230
+ let suspendedTenants = 0;
231
+
232
+ rows.forEach((t) => {
233
+ totalCashiers += Number(t.cashierAccountsCount || 0);
234
+
235
+ const isUnlimited = isUnlimitedCashierLimit(t.cashierLimit);
236
+ if (!isUnlimited) {
237
+ limitSum += Number(t.cashierLimit || 0);
238
+ }
239
+
240
+ const util = computeCashierUtilization(t);
241
+ if (util !== null) {
242
+ if (util >= 100) overLimitTenants++;
243
+ else if (util >= 80) nearLimitTenants++;
244
+ }
245
+
246
+ if (getPaymentStatus(t) === "overdue") overduePayments++;
247
+ if (new Date(t.createdAt) > thirtyDaysAgo) newTenantsLast30Days++;
248
+
249
+ if (t.status === "active") activeTenants++;
250
+ if (t.status === "suspended") suspendedTenants++;
251
+ });
252
+
253
+ let capacityUtilizationPercent = null;
254
+ if (limitSum > 0) {
255
+ capacityUtilizationPercent = Math.min(100, Math.round((totalCashiers / limitSum) * 100));
256
+ }
257
+
258
+ return {
259
+ totalTenants: totalAll,
260
+ totalVisibleTenants: totalVisible,
261
+ totalCashiers,
262
+ capacityUtilizationPercent,
263
+ nearLimitTenants,
264
+ overLimitTenants,
265
+ overduePayments,
266
+ newTenantsLast30Days,
267
+ activeTenants,
268
+ suspendedTenants,
269
+ };
270
+ };
271
+
272
  export const checkAdmin = async (req, res) => {
273
  try {
274
  const count = await User.countDocuments({ role: "super_admin" });