@fonderie/customers 4.0.0 → 5.1.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.d.cts CHANGED
@@ -160,6 +160,7 @@ declare class CustomerModel {
160
160
  /** Resolve a referral code to the referring customer's id, within a workspace. */
161
161
  resolveReferralCode(workspaceId: string, code: string): Promise<string | null>;
162
162
  list(opts: ListCustomersOpts): Promise<ICustomer[]>;
163
+ count(opts: Omit<ListCustomersOpts, 'limit' | 'offset'>): Promise<number>;
163
164
  findById(id: string, workspaceId: string): Promise<ICustomer | null>;
164
165
  findDetail(id: string, workspaceId: string, depth: 2): Promise<ICustomerDetailD2 | null>;
165
166
  findDetail(id: string, workspaceId: string, depth?: 1): Promise<ICustomerDetail | null>;
package/dist/index.d.ts CHANGED
@@ -160,6 +160,7 @@ declare class CustomerModel {
160
160
  /** Resolve a referral code to the referring customer's id, within a workspace. */
161
161
  resolveReferralCode(workspaceId: string, code: string): Promise<string | null>;
162
162
  list(opts: ListCustomersOpts): Promise<ICustomer[]>;
163
+ count(opts: Omit<ListCustomersOpts, 'limit' | 'offset'>): Promise<number>;
163
164
  findById(id: string, workspaceId: string): Promise<ICustomer | null>;
164
165
  findDetail(id: string, workspaceId: string, depth: 2): Promise<ICustomerDetailD2 | null>;
165
166
  findDetail(id: string, workspaceId: string, depth?: 1): Promise<ICustomerDetail | null>;
package/dist/index.js CHANGED
@@ -257,6 +257,28 @@ var CustomerModel = class {
257
257
  params
258
258
  );
259
259
  }
260
+ async count(opts) {
261
+ const conditions = ["workspace_id = $1"];
262
+ const params = [opts.workspaceId];
263
+ if (opts.blacklisted !== void 0) {
264
+ params.push(opts.blacklisted);
265
+ conditions.push(`is_blacklisted = $${params.length}`);
266
+ }
267
+ if (opts.search) {
268
+ params.push(`%${opts.search}%`);
269
+ const idx = params.length;
270
+ conditions.push(
271
+ `(first_name ILIKE $${idx} OR last_name ILIKE $${idx} OR company_name ILIKE $${idx} OR reference_code ILIKE $${idx})`
272
+ );
273
+ }
274
+ const [row] = await this.store.query(
275
+ `SELECT COUNT(*) AS count
276
+ FROM fonderie_customers
277
+ WHERE ${conditions.join(" AND ")}`,
278
+ params
279
+ );
280
+ return Number(row?.count ?? 0);
281
+ }
260
282
  async findById(id, workspaceId) {
261
283
  const [row] = await this.store.query(
262
284
  `SELECT ${SELECT_CUSTOMER}
@@ -1309,9 +1331,14 @@ function customerController(store, config = {}, bus) {
1309
1331
  if (archived !== null && archived !== void 0) {
1310
1332
  listOpts.blacklisted = archived === "true" || archived === "1";
1311
1333
  }
1312
- const list = await customers.list(listOpts);
1334
+ const { limit: _limit, offset: _offset, ...countOpts } = listOpts;
1335
+ const [list, total] = await Promise.all([
1336
+ customers.list(listOpts),
1337
+ customers.count(countOpts)
1338
+ ]);
1313
1339
  return setApiResponse(HTTP.OK, "CUSTOMERS_FETCHED", "Customers retrieved successfully.", {
1314
- customers: list.map(toCustomerDTO)
1340
+ customers: list.map(toCustomerDTO),
1341
+ total
1315
1342
  });
1316
1343
  },
1317
1344
  async get(ctx) {