@ikatec/digisac-api-sdk 2.0.0 → 2.1.1

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.
@@ -0,0 +1,196 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { TermsApi } from './TermsApi'
3
+ import type { ApiClient } from '../../core/ApiClient'
4
+ import type { Term, CreateTermPayload } from './types'
5
+ import type { Paginated } from '../../core/types'
6
+
7
+ // ── Mock client factory ──────────────────────────────────────────────
8
+
9
+ function createMockClient(): ApiClient {
10
+ return {
11
+ setAccessToken: vi.fn().mockReturnThis(),
12
+ request: vi.fn(),
13
+ get: vi.fn(),
14
+ post: vi.fn(),
15
+ put: vi.fn(),
16
+ patch: vi.fn(),
17
+ delete: vi.fn(),
18
+ }
19
+ }
20
+
21
+ // ── Fixtures ─────────────────────────────────────────────────────────
22
+
23
+ const term: Term = {
24
+ id: 'term-1',
25
+ title: { en: 'Terms of Service' },
26
+ text: { en: 'You agree to...' },
27
+ expirationDate: '2025-01-01',
28
+ version: '1.0',
29
+ agnusId: 'agnus-1',
30
+ accountId: null,
31
+ userId: null,
32
+ agreementDate: null,
33
+ createdAt: '2024-01-01T00:00:00Z',
34
+ updatedAt: '2024-01-01T00:00:00Z',
35
+ }
36
+
37
+ const paginated: Paginated<Term> = {
38
+ data: [term],
39
+ total: 1,
40
+ limit: 10,
41
+ skip: 0,
42
+ currentPage: 1,
43
+ lastPage: 1,
44
+ from: 1,
45
+ to: 1,
46
+ }
47
+
48
+ // ── Tests ────────────────────────────────────────────────────────────
49
+
50
+ describe('TermsApi', () => {
51
+ let client: ReturnType<typeof createMockClient>
52
+ let api: TermsApi
53
+
54
+ beforeEach(() => {
55
+ client = createMockClient()
56
+ api = new TermsApi(client)
57
+ })
58
+
59
+ // ── getMany ──────────────────────────────────────────────────────
60
+
61
+ describe('getMany', () => {
62
+ it('calls client.get with /terms when no query is provided', async () => {
63
+ vi.mocked(client.get).mockResolvedValue(paginated)
64
+ const result = await api.getMany()
65
+ expect(client.get).toHaveBeenCalledWith('/terms', undefined)
66
+ expect(result).toEqual(paginated)
67
+ })
68
+
69
+ it('serializes query parameters', async () => {
70
+ vi.mocked(client.get).mockResolvedValue(paginated)
71
+ await api.getMany({ page: 2 })
72
+ const url = vi.mocked(client.get).mock.calls[0][0] as string
73
+ expect(url).toContain('/terms?')
74
+ expect(url).toContain('page=2')
75
+ })
76
+
77
+ it('forwards custom headers', async () => {
78
+ vi.mocked(client.get).mockResolvedValue(paginated)
79
+ const headers = { 'X-Custom': 'value' }
80
+ await api.getMany(undefined, headers)
81
+ expect(client.get).toHaveBeenCalledWith('/terms', headers)
82
+ })
83
+ })
84
+
85
+ // ── getById ──────────────────────────────────────────────────────
86
+
87
+ describe('getById', () => {
88
+ it('calls client.get with /terms/:id', async () => {
89
+ vi.mocked(client.get).mockResolvedValue(term)
90
+ const result = await api.getById('term-1')
91
+ expect(client.get).toHaveBeenCalledWith('/terms/term-1', undefined)
92
+ expect(result).toEqual(term)
93
+ })
94
+
95
+ it('forwards custom headers', async () => {
96
+ vi.mocked(client.get).mockResolvedValue(term)
97
+ const headers = { 'X-Tenant': 'abc' }
98
+ await api.getById('term-1', undefined, headers)
99
+ expect(client.get).toHaveBeenCalledWith('/terms/term-1', headers)
100
+ })
101
+ })
102
+
103
+ // ── create ───────────────────────────────────────────────────────
104
+
105
+ describe('create', () => {
106
+ it('calls client.post with /terms and body', async () => {
107
+ vi.mocked(client.post).mockResolvedValue(term)
108
+ const body: CreateTermPayload = {
109
+ title: { en: 'ToS' },
110
+ text: { en: 'Text' },
111
+ expirationDate: '2025-01-01',
112
+ version: '1.0',
113
+ agnusId: 'agnus-1',
114
+ }
115
+ const result = await api.create(body)
116
+ expect(client.post).toHaveBeenCalledWith('/terms', body, undefined)
117
+ expect(result).toEqual(term)
118
+ })
119
+ })
120
+
121
+ // ── updateById ───────────────────────────────────────────────────
122
+
123
+ describe('updateById', () => {
124
+ it('calls client.put with /terms/:id and body', async () => {
125
+ vi.mocked(client.put).mockResolvedValue(term)
126
+ const result = await api.updateById('term-1', { version: '1.1' })
127
+ expect(client.put).toHaveBeenCalledWith('/terms/term-1', { version: '1.1' }, undefined)
128
+ expect(result).toEqual(term)
129
+ })
130
+
131
+ it('forwards custom headers', async () => {
132
+ vi.mocked(client.put).mockResolvedValue(term)
133
+ const headers = { 'If-Match': 'etag' }
134
+ await api.updateById('term-1', { version: '2.0' }, headers)
135
+ expect(client.put).toHaveBeenCalledWith('/terms/term-1', { version: '2.0' }, headers)
136
+ })
137
+ })
138
+
139
+ // ── deleteById ───────────────────────────────────────────────────
140
+
141
+ describe('deleteById', () => {
142
+ it('calls client.delete with /terms/:id', async () => {
143
+ vi.mocked(client.delete).mockResolvedValue(term)
144
+ const result = await api.deleteById('term-1')
145
+ expect(client.delete).toHaveBeenCalledWith('/terms/term-1', undefined)
146
+ expect(result).toEqual(term)
147
+ })
148
+
149
+ it('forwards custom headers', async () => {
150
+ vi.mocked(client.delete).mockResolvedValue(term)
151
+ const headers = { 'X-Reason': 'cleanup' }
152
+ await api.deleteById('term-1', headers)
153
+ expect(client.delete).toHaveBeenCalledWith('/terms/term-1', headers)
154
+ })
155
+ })
156
+
157
+ // ── getNotAgreed ─────────────────────────────────────────────────
158
+
159
+ describe('getNotAgreed', () => {
160
+ it('calls client.get with /terms/terms-not-agreed when no query is provided', async () => {
161
+ vi.mocked(client.get).mockResolvedValue(paginated)
162
+ const result = await api.getNotAgreed()
163
+ expect(client.get).toHaveBeenCalledWith('/terms/terms-not-agreed', undefined)
164
+ expect(result).toEqual(paginated)
165
+ })
166
+
167
+ it('serializes query parameters', async () => {
168
+ vi.mocked(client.get).mockResolvedValue(paginated)
169
+ await api.getNotAgreed({ page: 1, perPage: 5 })
170
+ const url = vi.mocked(client.get).mock.calls[0][0] as string
171
+ expect(url).toContain('/terms/terms-not-agreed?')
172
+ expect(url).toContain('page=1')
173
+ })
174
+
175
+ it('forwards custom headers', async () => {
176
+ vi.mocked(client.get).mockResolvedValue(paginated)
177
+ const headers = { 'X-Custom': 'h' }
178
+ await api.getNotAgreed(undefined, headers)
179
+ expect(client.get).toHaveBeenCalledWith('/terms/terms-not-agreed', headers)
180
+ })
181
+ })
182
+
183
+ // ── Error propagation ────────────────────────────────────────────
184
+
185
+ describe('error propagation', () => {
186
+ it('propagates errors from getMany', async () => {
187
+ vi.mocked(client.get).mockRejectedValue(new Error('Network error'))
188
+ await expect(api.getMany()).rejects.toThrow('Network error')
189
+ })
190
+
191
+ it('propagates errors from getNotAgreed', async () => {
192
+ vi.mocked(client.get).mockRejectedValue(new Error('Forbidden'))
193
+ await expect(api.getNotAgreed()).rejects.toThrow('Forbidden')
194
+ })
195
+ })
196
+ })
@@ -0,0 +1,63 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { TranscriptsApi } from './TranscriptsApi'
3
+ import type { ApiClient } from '../../core/ApiClient'
4
+ import type { TranscriptResponse } from './types'
5
+
6
+ // ── Mock client factory ──────────────────────────────────────────────
7
+
8
+ function createMockClient(): ApiClient {
9
+ return {
10
+ setAccessToken: vi.fn().mockReturnThis(),
11
+ request: vi.fn(),
12
+ get: vi.fn(),
13
+ post: vi.fn(),
14
+ put: vi.fn(),
15
+ patch: vi.fn(),
16
+ delete: vi.fn(),
17
+ }
18
+ }
19
+
20
+ // ── Tests ────────────────────────────────────────────────────────────
21
+
22
+ describe('TranscriptsApi', () => {
23
+ let client: ReturnType<typeof createMockClient>
24
+ let api: TranscriptsApi
25
+
26
+ beforeEach(() => {
27
+ client = createMockClient()
28
+ api = new TranscriptsApi(client)
29
+ })
30
+
31
+ // ── getById ──────────────────────────────────────────────────────
32
+
33
+ describe('getById', () => {
34
+ it('calls client.get with /transcripts/:id', async () => {
35
+ const response: TranscriptResponse = {
36
+ id: 'msg-1',
37
+ isTranscribing: true,
38
+ transcribeError: false,
39
+ }
40
+ vi.mocked(client.get).mockResolvedValue(response)
41
+ const result = await api.getById('msg-1')
42
+ expect(client.get).toHaveBeenCalledWith('/transcripts/msg-1', undefined)
43
+ expect(result).toEqual(response)
44
+ })
45
+
46
+ it('forwards custom headers', async () => {
47
+ const response: TranscriptResponse = {
48
+ id: 'msg-2',
49
+ isTranscribing: false,
50
+ transcribeError: true,
51
+ }
52
+ vi.mocked(client.get).mockResolvedValue(response)
53
+ const headers = { 'X-Custom': 'h' }
54
+ await api.getById('msg-2', headers)
55
+ expect(client.get).toHaveBeenCalledWith('/transcripts/msg-2', headers)
56
+ })
57
+
58
+ it('propagates errors from client.get', async () => {
59
+ vi.mocked(client.get).mockRejectedValue(new Error('Not found'))
60
+ await expect(api.getById('missing')).rejects.toThrow('Not found')
61
+ })
62
+ })
63
+ })
@@ -4,11 +4,32 @@ export type WhatsappBusinessTemplateLanguage = 'pt_BR' | 'es' | 'en'
4
4
 
5
5
  export type WhatsappBusinessTemplateCategory = 'AUTHENTICATION' | 'MARKETING' | 'UTILITY'
6
6
 
7
+ export type WhatsappBusinessTemplateComponentType = 'HEADER' | 'BODY' | 'FOOTER' | 'BUTTONS'
8
+
9
+ export type WhatsappBusinessTemplateHeaderFormat = 'TEXT' | 'IMAGE' | 'VIDEO' | 'DOCUMENT'
10
+
11
+ export type WhatsappBusinessTemplateButton = {
12
+ type: string
13
+ text: string
14
+ url?: string
15
+ params?: string[]
16
+ phone_number?: string
17
+ }
18
+
19
+ export type WhatsappBusinessTemplateComponent = {
20
+ type: WhatsappBusinessTemplateComponentType
21
+ format?: WhatsappBusinessTemplateHeaderFormat
22
+ text?: string
23
+ params?: string[]
24
+ buttons?: WhatsappBusinessTemplateButton[]
25
+ code_expiration_minutes?: number
26
+ }
27
+
7
28
  export type WhatsappBusinessTemplate = {
8
29
  id: string
9
30
  internalName: string | null
10
31
  category: string
11
- components: Record<string, unknown>[]
32
+ components: WhatsappBusinessTemplateComponent[]
12
33
  language: string
13
34
  name: string
14
35
  namespace: string
@@ -28,7 +49,7 @@ export type WhatsappBusinessTemplate = {
28
49
  export type CreateWhatsappBusinessTemplatePayload = {
29
50
  name: string
30
51
  internalName: string
31
- components: Record<string, unknown>[]
52
+ components: WhatsappBusinessTemplateComponent[]
32
53
  serviceId: string
33
54
  messageType: WhatsappBusinessTemplateMessageType
34
55
  language: WhatsappBusinessTemplateLanguage
@@ -38,7 +59,7 @@ export type CreateWhatsappBusinessTemplatePayload = {
38
59
  export type UpdateWhatsappBusinessTemplatePayload = {
39
60
  name?: string
40
61
  internalName?: string
41
- components?: Record<string, unknown>[]
62
+ components?: WhatsappBusinessTemplateComponent[]
42
63
  serviceId?: string
43
64
  messageType?: WhatsappBusinessTemplateMessageType
44
65
  language?: WhatsappBusinessTemplateLanguage
@@ -21,8 +21,8 @@ export class BaseCrudApi<
21
21
  TUpdate = TCreate,
22
22
  TQuery extends ListQuery<TResponse> = ListQuery<TResponse>,
23
23
  > implements CrudApi<TResponse, TCreate, TUpdate, TQuery> {
24
- protected readonly client: ApiClient
25
- protected readonly basePath: string
24
+ public readonly client: ApiClient
25
+ public readonly basePath: string
26
26
 
27
27
  constructor(client: ApiClient, basePath: string) {
28
28
  this.client = client