@fonderie/customers 5.0.0 → 6.0.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/README.md CHANGED
@@ -24,7 +24,7 @@ elsewhere in your app.
24
24
  You've shipped this plumbing before — auth, teams, billing, messaging —
25
25
  and the next project will ask for it again. Fonderie packages it once:
26
26
  plain TypeScript modules for
27
- [`@fonderie/core`](https://github.com/fonderie-js/sdk/tree/main/packages/core),
27
+ [`@fonderie/core`](https://github.com/fonderiejs/sdk/tree/main/packages/core),
28
28
  PostgreSQL-backed, self-hosted, MIT. No external control plane, no
29
29
  per-seat anything. Register the modules you need; skip the ones you don't.
30
30
 
@@ -33,7 +33,7 @@ individuals and businesses with their emails, phones, addresses, notes,
33
33
  and tags.
34
34
 
35
35
  Browse the whole set at
36
- [fonderie-js/sdk](https://github.com/fonderie-js/sdk) · follow
36
+ [fonderiejs/sdk](https://github.com/fonderiejs/sdk) · follow
37
37
  [@fonderiejs](https://x.com/fonderiejs)
38
38
 
39
39
  ## License
@@ -123,6 +123,7 @@ new CustomerEmailModel(store: IStoreAdapter): CustomerEmailModel
123
123
  new CustomerModel(store: IStoreAdapter): CustomerModel
124
124
  .resolveReferralCode(workspaceId: string, code: string): Promise<string | null>
125
125
  .list(opts: ListCustomersOpts): Promise<ICustomer[]>
126
+ .count(opts: Omit<ListCustomersOpts, "limit" | "offset">): Promise<number>
126
127
  .findById(id: string, workspaceId: string): Promise<ICustomer | null>
127
128
  .findDetail(id: string, workspaceId: string, depth: 2): Promise<ICustomerDetailD2 | null>
128
129
  .create(opts: CreateCustomerOpts): Promise<ICustomer>
@@ -154,12 +155,8 @@ new CustomersModule(store: IStoreAdapter, config?: ICustomersConfig, bus?: Event
154
155
  .deps: string[]
155
156
  .install(app: IFonderieApp): void
156
157
 
157
- type AddressLabel = 'service' | 'billing' | 'other';
158
-
159
158
  type CustomerType = 'individual' | 'business';
160
159
 
161
- type EmailLabel = 'work' | 'personal' | 'billing';
162
-
163
160
  interface IAddress {
164
161
  id: string;
165
162
  countryIso: string;
@@ -243,7 +240,5 @@ interface ICustomerTag {
243
240
  tag: string;
244
241
  }
245
242
 
246
- type PhoneLabel = 'mobile' | 'office' | 'home' | 'fax';
247
-
248
243
  namespace schemas — exports: addAddressSchema, addEmailSchema, addPhoneSchema, addRelationshipSchema, addTagSchema, blacklistSchema, createCustomerSchema, noteSchema, updateAddressSchema, updateCustomerSchema, updateEmailSchema, updatePhoneSchema
249
244
  ```
package/dist/index.cjs CHANGED
@@ -293,6 +293,28 @@ var CustomerModel = class {
293
293
  params
294
294
  );
295
295
  }
296
+ async count(opts) {
297
+ const conditions = ["workspace_id = $1"];
298
+ const params = [opts.workspaceId];
299
+ if (opts.blacklisted !== void 0) {
300
+ params.push(opts.blacklisted);
301
+ conditions.push(`is_blacklisted = $${params.length}`);
302
+ }
303
+ if (opts.search) {
304
+ params.push(`%${opts.search}%`);
305
+ const idx = params.length;
306
+ conditions.push(
307
+ `(first_name ILIKE $${idx} OR last_name ILIKE $${idx} OR company_name ILIKE $${idx} OR reference_code ILIKE $${idx})`
308
+ );
309
+ }
310
+ const [row] = await this.store.query(
311
+ `SELECT COUNT(*) AS count
312
+ FROM fonderie_customers
313
+ WHERE ${conditions.join(" AND ")}`,
314
+ params
315
+ );
316
+ return Number(row?.count ?? 0);
317
+ }
296
318
  async findById(id, workspaceId) {
297
319
  const [row] = await this.store.query(
298
320
  `SELECT ${SELECT_CUSTOMER}
@@ -1345,9 +1367,14 @@ function customerController(store, config = {}, bus) {
1345
1367
  if (archived !== null && archived !== void 0) {
1346
1368
  listOpts.blacklisted = archived === "true" || archived === "1";
1347
1369
  }
1348
- const list = await customers.list(listOpts);
1370
+ const { limit: _limit, offset: _offset, ...countOpts } = listOpts;
1371
+ const [list, total] = await Promise.all([
1372
+ customers.list(listOpts),
1373
+ customers.count(countOpts)
1374
+ ]);
1349
1375
  return (0, import_core2.setApiResponse)(import_core2.HTTP.OK, "CUSTOMERS_FETCHED", "Customers retrieved successfully.", {
1350
- customers: list.map(toCustomerDTO)
1376
+ customers: list.map(toCustomerDTO),
1377
+ total
1351
1378
  });
1352
1379
  },
1353
1380
  async get(ctx) {