core-services-sdk 1.3.63 → 1.3.65

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.
Files changed (35) hide show
  1. package/package.json +1 -1
  2. package/src/core/normalize-phone-number.js +144 -25
  3. package/src/postgresql/core/get-table-name.js +10 -0
  4. package/src/postgresql/filters/apply-filter-object.js +19 -0
  5. package/src/postgresql/filters/apply-filter-snake-case.js +16 -0
  6. package/src/postgresql/filters/apply-filter.js +33 -0
  7. package/src/postgresql/filters/operators.js +32 -0
  8. package/src/postgresql/index.js +5 -2
  9. package/src/postgresql/modifiers/apply-order-by.js +19 -0
  10. package/src/postgresql/modifiers/apply-pagination.js +12 -0
  11. package/src/postgresql/pagination/paginate.js +48 -0
  12. package/tests/core/normalize-phone-number.unit.test.js +45 -0
  13. package/tests/postgresql/apply-filter-snake-case.integration.test.js +220 -0
  14. package/tests/postgresql/apply-filter.integration.test.js +34 -353
  15. package/tests/postgresql/core/get-table-name.unit.test.js +20 -0
  16. package/tests/postgresql/filters/apply-filter-object.test.js +23 -0
  17. package/tests/postgresql/filters/operators.unit.test.js +23 -0
  18. package/tests/postgresql/modifiers/apply-order-by.test.js +80 -0
  19. package/tests/postgresql/modifiers/apply-pagination.unit.test.js +18 -0
  20. package/tests/postgresql/paginate.integration.test.js +10 -18
  21. package/tests/postgresql/pagination/paginate.js +48 -0
  22. package/tests/postgresql/validate-schema.integration.test.js +9 -5
  23. package/types/core/normalize-phone-number.d.ts +97 -27
  24. package/types/postgresql/apply-filter.d.ts +131 -20
  25. package/types/postgresql/core/get-table-name.d.ts +10 -0
  26. package/types/postgresql/filters/apply-filter-object.d.ts +15 -0
  27. package/types/postgresql/filters/apply-filter-snake-case.d.ts +14 -0
  28. package/types/postgresql/filters/apply-filter.d.ts +15 -0
  29. package/types/postgresql/filters/operators.d.ts +14 -0
  30. package/types/postgresql/index.d.ts +5 -2
  31. package/types/postgresql/modifiers/apply-order-by.d.ts +17 -0
  32. package/types/postgresql/modifiers/apply-pagination.d.ts +17 -0
  33. package/types/postgresql/pagination/paginate.d.ts +29 -0
  34. package/src/postgresql/apply-filter.js +0 -275
  35. package/src/postgresql/paginate.js +0 -61
@@ -0,0 +1,220 @@
1
+ // @ts-nocheck
2
+ import { describe, it, beforeAll, afterAll, beforeEach, expect } from 'vitest'
3
+ import knex from 'knex'
4
+
5
+ import {
6
+ stopPostgres,
7
+ startPostgres,
8
+ buildPostgresUri,
9
+ } from '../../src/postgresql/start-stop-postgres-docker.js'
10
+
11
+ import { applyFilterSnakeCase } from '../../src/postgresql/filters/apply-filter-snake-case.js'
12
+
13
+ const PG_OPTIONS = {
14
+ port: 5444,
15
+ containerName: 'postgres-apply-filter-test-5444',
16
+ user: 'testuser',
17
+ pass: 'testpass',
18
+ db: 'testdb',
19
+ }
20
+
21
+ const DATABASE_URI = buildPostgresUri(PG_OPTIONS)
22
+
23
+ let db
24
+
25
+ beforeAll(async () => {
26
+ startPostgres(PG_OPTIONS)
27
+
28
+ db = knex({
29
+ client: 'pg',
30
+ connection: DATABASE_URI,
31
+ })
32
+
33
+ await db.schema.createTable('assets', (table) => {
34
+ table.uuid('id').primary()
35
+ table.string('name').notNullable()
36
+ table.string('status')
37
+ table.string('type')
38
+ table.decimal('price', 10, 2)
39
+ table.timestamp('created_at').notNullable()
40
+ table.timestamp('deleted_at')
41
+ })
42
+
43
+ await db.schema.createTable('invoices', (table) => {
44
+ table.uuid('id').primary()
45
+ table.string('invoice_number').notNullable()
46
+ table.decimal('amount', 10, 2).notNullable()
47
+ table.string('status')
48
+ table.uuid('customer_id')
49
+ table.timestamp('created_at').notNullable()
50
+ table.timestamp('paid_at')
51
+ table.timestamp('deleted_at')
52
+ })
53
+ })
54
+
55
+ afterAll(async () => {
56
+ if (db) {
57
+ await db.destroy()
58
+ }
59
+ stopPostgres(PG_OPTIONS.containerName)
60
+ })
61
+
62
+ beforeEach(async () => {
63
+ await db('assets').truncate()
64
+ await db('invoices').truncate()
65
+
66
+ await db('assets').insert([
67
+ {
68
+ id: '00000000-0000-0000-0000-000000000001',
69
+ name: 'Asset One',
70
+ status: 'active',
71
+ type: 'invoice',
72
+ price: 100,
73
+ created_at: new Date('2024-01-01'),
74
+ deleted_at: null,
75
+ },
76
+ {
77
+ id: '00000000-0000-0000-0000-000000000002',
78
+ name: 'Asset Two',
79
+ status: 'active',
80
+ type: 'receipt',
81
+ price: 200,
82
+ created_at: new Date('2024-01-02'),
83
+ deleted_at: null,
84
+ },
85
+ {
86
+ id: '00000000-0000-0000-0000-000000000003',
87
+ name: 'Asset Three',
88
+ status: 'pending',
89
+ type: 'invoice',
90
+ price: 150,
91
+ created_at: new Date('2024-01-03'),
92
+ deleted_at: null,
93
+ },
94
+ {
95
+ id: '00000000-0000-0000-0000-000000000004',
96
+ name: 'Deleted Asset',
97
+ status: 'deleted',
98
+ type: 'receipt',
99
+ price: 50,
100
+ created_at: new Date('2024-01-04'),
101
+ deleted_at: new Date('2024-01-05'),
102
+ },
103
+ {
104
+ id: '00000000-0000-0000-0000-000000000005',
105
+ name: 'Expensive Asset',
106
+ status: 'active',
107
+ type: 'invoice',
108
+ price: 500,
109
+ created_at: new Date('2024-01-05'),
110
+ deleted_at: null,
111
+ },
112
+ ])
113
+
114
+ await db('invoices').insert([
115
+ {
116
+ id: '00000000-0000-0000-0000-000000000101',
117
+ invoice_number: 'INV-001',
118
+ amount: 1000,
119
+ status: 'paid',
120
+ customer_id: '00000000-0000-0000-0000-000000000201',
121
+ created_at: new Date('2024-01-01'),
122
+ paid_at: new Date('2024-01-02'),
123
+ deleted_at: null,
124
+ },
125
+ {
126
+ id: '00000000-0000-0000-0000-000000000102',
127
+ invoice_number: 'INV-002',
128
+ amount: 2500,
129
+ status: 'pending',
130
+ customer_id: '00000000-0000-0000-0000-000000000202',
131
+ created_at: new Date('2024-01-02'),
132
+ paid_at: null,
133
+ deleted_at: null,
134
+ },
135
+ {
136
+ id: '00000000-0000-0000-0000-000000000103',
137
+ invoice_number: 'INV-003',
138
+ amount: 500,
139
+ status: 'paid',
140
+ customer_id: '00000000-0000-0000-0000-000000000201',
141
+ created_at: new Date('2024-01-03'),
142
+ paid_at: new Date('2024-01-04'),
143
+ deleted_at: null,
144
+ },
145
+ {
146
+ id: '00000000-0000-0000-0000-000000000104',
147
+ invoice_number: 'INV-004',
148
+ amount: 3000,
149
+ status: 'overdue',
150
+ customer_id: '00000000-0000-0000-0000-000000000203',
151
+ created_at: new Date('2024-01-04'),
152
+ paid_at: null,
153
+ deleted_at: null,
154
+ },
155
+ {
156
+ id: '00000000-0000-0000-0000-000000000105',
157
+ invoice_number: 'INV-005',
158
+ amount: 750,
159
+ status: 'cancelled',
160
+ customer_id: '00000000-0000-0000-0000-000000000201',
161
+ created_at: new Date('2024-01-05'),
162
+ paid_at: null,
163
+ deleted_at: new Date('2024-01-06'),
164
+ },
165
+ ])
166
+ })
167
+
168
+ describe('applyFilterSnakeCase integration', () => {
169
+ it('applies equality filter', async () => {
170
+ const query = db('assets').select('*')
171
+
172
+ applyFilterSnakeCase({
173
+ query,
174
+ filter: { status: 'active' },
175
+ })
176
+
177
+ const results = await query
178
+ expect(results).toHaveLength(3)
179
+ })
180
+
181
+ it('converts camelCase keys to snake_case', async () => {
182
+ const query = db('assets').select('*')
183
+
184
+ applyFilterSnakeCase({
185
+ query,
186
+ filter: { deletedAt: { isNull: true }, name: 'Asset One' },
187
+ })
188
+
189
+ const results = await query
190
+ expect(results).toHaveLength(1)
191
+ expect(results[0].name).toBe('Asset One')
192
+ })
193
+
194
+ it('ignores unknown operators', async () => {
195
+ const query = db('assets').select('*')
196
+
197
+ applyFilterSnakeCase({
198
+ query,
199
+ filter: { status: { unknown: 'x' } },
200
+ })
201
+
202
+ const results = await query
203
+ expect(results).toHaveLength(5)
204
+ })
205
+
206
+ it('works with invoices table and camelCase conversion', async () => {
207
+ const query = db('invoices').select('*')
208
+
209
+ applyFilterSnakeCase({
210
+ query,
211
+ filter: {
212
+ customerId: '00000000-0000-0000-0000-000000000201',
213
+ amount: { gte: 500 },
214
+ },
215
+ })
216
+
217
+ const results = await query
218
+ expect(results).toHaveLength(3)
219
+ })
220
+ })