Skip to content

Commit 3321abf

Browse files
authored
feat: Typescript type for "setExternalData" (#807)
* Basic test * Typescript type * Separate model file * Not using "any" * Prettier
1 parent f9123e1 commit 3321abf

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export type ExternalData = {
2+
fields?: ExternalDataField[];
3+
};
4+
5+
export type ExternalDataField =
6+
| ExternalMessageField
7+
| ExternalTextualField
8+
| ExternalListField;
9+
10+
export interface ExternalMessageField {
11+
type: 'message';
12+
content: string;
13+
importance: 'info' | 'warning' | 'error';
14+
}
15+
16+
export interface ExternalTextualField {
17+
type: 'text' | 'email' | 'phoneNumber';
18+
title: string;
19+
value: string;
20+
}
21+
22+
export interface ExternalListItem {
23+
title: string;
24+
text?: string;
25+
url?: string;
26+
footer?: string;
27+
timestamp?: string;
28+
}
29+
30+
export interface ExternalListField {
31+
type: 'list';
32+
title: string;
33+
items: ExternalListItem[];
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as sinon from 'sinon';
2+
import { Qminder } from '../../qminder';
3+
import { TicketService } from './ticket.service';
4+
import { ExternalData } from '../../model/ticket/external-data.js';
5+
6+
describe('Ticket.setExternalData', () => {
7+
let requestStub: sinon.SinonStub;
8+
9+
beforeEach(() => {
10+
Qminder.setKey('EXAMPLE_API_KEY');
11+
Qminder.setServer('api.qminder.com');
12+
requestStub = sinon.stub(Qminder.ApiBase, 'request');
13+
});
14+
15+
afterEach(() => {
16+
sinon.restore();
17+
});
18+
19+
it('sends POST to v1/tickets/<id>/external with stringified data and returns success', async () => {
20+
const ticketId = '123';
21+
const provider = 'crm';
22+
const title = 'Case #42';
23+
const data: ExternalData = {
24+
fields: [
25+
{ type: 'message', content: 'Hello', importance: 'info' },
26+
{ type: 'text', title: 'Order', value: '42' },
27+
{
28+
type: 'list',
29+
title: 'Links',
30+
items: [{ title: 'Home', url: 'https://example.com' }],
31+
},
32+
],
33+
};
34+
35+
requestStub.resolves({ result: 'success' });
36+
37+
const result = await TicketService.setExternalData(
38+
ticketId,
39+
provider,
40+
title,
41+
data,
42+
);
43+
44+
expect(result).toBe('success');
45+
46+
expect(requestStub.calledOnce).toBeTruthy();
47+
const [url, options] = requestStub.getCall(0).args as [string, any];
48+
expect(url).toBe(`v1/tickets/${ticketId}/external`);
49+
expect(options.method).toBe('POST');
50+
expect(options.body).toEqual({
51+
provider,
52+
title,
53+
data: JSON.stringify(data),
54+
});
55+
});
56+
});

packages/javascript-api/src/lib/services/ticket/ticket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../../util/id-or-object.js';
1212
import { ApiBase } from '../api-base/api-base.js';
1313
import { ResponseValidationError } from '../../model/errors/response-validation-error.js';
14+
import { ExternalData } from '../../model/ticket/external-data.js';
1415

1516
/**
1617
* Represents a collection of search criteria for TicketService.count().
@@ -755,7 +756,7 @@ export function setExternalData(
755756
ticket: IdOrObject<Ticket>,
756757
provider: string,
757758
title: string,
758-
data: any,
759+
data: ExternalData,
759760
): Promise<'success'> {
760761
const ticketId = extractId(ticket);
761762

0 commit comments

Comments
 (0)