Skip to content

Commit

Permalink
feat: add contacts query (#108)
Browse files Browse the repository at this point in the history
* feat: add contacts query

* feat: add test
  • Loading branch information
braaar authored Sep 8, 2023
1 parent 6c1d33c commit 8c6d6d3
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/calls/contact/__tests__/contact.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { endOfMonth, startOfMonth } from 'date-fns';
import nock from 'nock';
import { invariant } from 'ts-invariant';
import { parseRuntypeValidationError } from '../../../utils';
import { TripletexContact } from '../contact';

const baseUrl = process.env.BASE_URL || 'https://api.tripletex.io/';
const sessionToken = process.env.SESSION_TOKEN || 'a-session-token';

describe('activity class', () => {
let client: TripletexContact;

beforeEach(() => {
client = new TripletexContact({ baseUrl }, sessionToken);
});

it('should list contacts', async () => {
nock('https://api.tripletex.io:443', { encodedQueryParams: true })
.get('/v2/contact')
.reply(200, {
fullResultSize: 1,
from: 0,
count: 1,
versionDigest: 'Checksum not yet supported for this endpoint',
values: [
{
id: 1734852,
version: 1,
url: 'api.tripletex.io/v2/contact/1734852',
firstName: 'Simen A. W. Olsen',
lastName: 'API Testuser',
displayName: 'Simen A. W. Olsen API Testuser',
email: '[email protected]',
phoneNumberMobileCountry: {
id: 161,
url: 'api.tripletex.io/v2/country/161',
},
phoneNumberMobile: '',
phoneNumberWork: '',
customer: {
id: 88885,
url: 'api.tripletex.io/v2/customer/88885',
},
department: {
id: 88885,
url: 'api.tripletex.io/v2/department/88885',
},
isInactive: false,
},
],
});
const entries = await client.list();

parseRuntypeValidationError(entries.error);
invariant(entries.success);
expect(entries.body.values).toMatchInlineSnapshot(`
Array [
Object {
"customer": Object {
"id": 88885,
"url": "api.tripletex.io/v2/customer/88885",
},
"department": Object {
"id": 88885,
"url": "api.tripletex.io/v2/department/88885",
},
"displayName": "Simen A. W. Olsen API Testuser",
"email": "[email protected]",
"firstName": "Simen A. W. Olsen",
"id": 1734852,
"isInactive": false,
"lastName": "API Testuser",
"phoneNumberMobile": "",
"phoneNumberMobileCountry": Object {
"id": 161,
"url": "api.tripletex.io/v2/country/161",
},
"phoneNumberWork": "",
"url": "api.tripletex.io/v2/contact/1734852",
"version": 1,
},
]
`);
});
});
77 changes: 77 additions & 0 deletions src/calls/contact/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DefaultTripletexInputs } from "../../types";
import { serializeQuery, withRuntype } from "../../utils";
import { TripletexBase } from "../base";
import { listContactsResponseRt } from "./models/contact";

export interface ListContactsInput extends DefaultTripletexInputs {
/**
* List of IDs
*/
id?: string[];

/**
* Containing
*/
firstName?: string;

/**
* Containing
*/
lastName?: string;

/**
* Containing
*/
email?: string;

/**
* List of IDs
*/
customerId?: string;

/**
* List of IDs
*/
departmentId?: string;

/**
* From index
*/
includeContacts?: boolean;

/**
* List of IDs
*/
deploymentId?: string[];

/**
* Number of elements to return
*/
count: number;

/**
* Sorting pattern
*/
sorting: string;

/**
* Fields filter pattern
*/
fields: string;
}

export class TripletexContact extends TripletexBase {
list(input?: ListContactsInput) {
const call = this.authenticatedCall() //
.args<{
input?: ListContactsInput;
}>()
.path('/v2/contact')
.query(args => (args.input ? serializeQuery(args.input) : {}))
.method('get')
.parseJson(withRuntype(listContactsResponseRt))
.build();

return this.performRequest(sessionToken => call({ input, sessionToken }));
}
}
34 changes: 34 additions & 0 deletions src/calls/contact/models/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as rt from 'runtypes';
import { multipleValuesEnvelope } from '../../../utils';

const contactRt = rt.Record({
id: rt.Number,
version: rt.Number.nullable().optional(),
url: rt.String,
firstName: rt.String,
lastName: rt.String,
displayName: rt.String.nullable().optional(),
email: rt.String.nullable().optional(),
phoneNumberMobileCountry: rt
.Record({
id: rt.Number,
})
.nullable()
.optional(),
phoneNumberMobile: rt.String.nullable().optional(),
phoneNumberWork: rt.String.nullable().optional(),
customer: rt.Record({
id: rt.Number,
url: rt.String,
}),
department: rt.Record({
id: rt.Number,
url: rt.String,
}),
isInactive: rt.Boolean.nullable().optional(),
})

export type Contact = rt.Static<typeof contactRt>;


export const listContactsResponseRt = multipleValuesEnvelope(contactRt);

0 comments on commit 8c6d6d3

Please sign in to comment.