Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add contacts query #108

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);