Skip to content

Commit

Permalink
Add createCompanion method
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Aug 11, 2023
1 parent a2fbd63 commit b9056fe
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 329 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"error",
"never"
],
"no-console": "error"
"no-console": "error",
"@typescript-eslint/no-explicit-any": "off"
}
}
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ const { data } = await new Guest('event-uuid').create({
})
```

#### Create a new companion guest

```javascript
import { Guest } from '@airlst/sdk'

const { data } = await new Guest('event-uuid').createCompanion('guest-code', {
contact: {
first_name: 'John',
last_name: 'Doe',
}
})
```

#### Update existing guest

```javascript
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airlst/sdk",
"version": "0.0.4",
"version": "0.0.5",
"repository": {
"type": "git",
"url": "git+https://github.com/airlst/sdk-js.git"
Expand All @@ -23,13 +23,13 @@
"check": "yarn run format && yarn run lint"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"prettier": "^2.8.8",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"eslint": "^8.46.0",
"eslint-config-prettier": "^9.0.0",
"prettier": "^3.0.1",
"typescript": "^5.1.6",
"vitest": "^0.32.2",
"vitest": "^0.34.1",
"vitest-fetch-mock": "^0.2.2"
}
}
2 changes: 1 addition & 1 deletion src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Api = class {

public static async sendRequest(
uri: string,
options?: RequestInit
options?: RequestInit,
): Promise<ResponseInterface> {
const response = await fetch(this.BASE_URL + uri, {
headers: {
Expand Down
5 changes: 3 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ export interface GuestInterface {
code: string
role: string
status: string
extended_fields: object
booking: BookingInterface
contact: ContactInterface
}

interface BookingInterface {
export interface BookingInterface {
extended_fields: object
}

interface ContactInterface {
export interface ContactInterface {
sex: string
full_name: string
first_name: string
Expand Down
37 changes: 31 additions & 6 deletions src/resources/Guest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Api } from '../Api'
import { GuestInterface } from '../interfaces'
import {
GuestInterface,
BookingInterface,
ContactInterface,
} from '../interfaces'

export const Guest = class {
public eventId: string
Expand All @@ -9,14 +13,14 @@ export const Guest = class {
}

public async validateCode(
code: string
code: string,
): Promise<ValidateCodeResponseInterface> {
return await Api.sendRequest(
`/events/${this.eventId}/guests/validate-code`,
{
method: 'post',
body: JSON.stringify({ code }),
}
},
)
}

Expand All @@ -25,17 +29,30 @@ export const Guest = class {
}

public async create(
body: CreateBodyInterface
body: CreateMainBodyInterface,
): Promise<CreateResponseInterface> {
return await Api.sendRequest(`/events/${this.eventId}/guests`, {
method: 'post',
body: JSON.stringify(body),
})
}

public async createCompanion(
code: string,
body: CreateCompanionBodyInterface,
): Promise<CreateResponseInterface> {
return await Api.sendRequest(
`/events/${this.eventId}/guests/${code}/companions`,
{
method: 'post',
body: JSON.stringify(body),
},
)
}

public async update(
code: string,
body: UpdateBodyInterface
body: UpdateBodyInterface,
): Promise<UpdateResponseInterface> {
return await Api.sendRequest(`/events/${this.eventId}/guests/${code}`, {
method: 'put',
Expand All @@ -62,10 +79,18 @@ interface CreateResponseInterface {
}
}

interface CreateBodyInterface {
interface CreateMainBodyInterface extends CreateCompanionBodyInterface {
status: string
}

interface CreateCompanionBodyInterface {
code: string
role: string
extended_fields: object
contact: ContactInterface
booking: BookingInterface
}

interface UpdateResponseInterface {
data: {
guest: GuestInterface
Expand Down
2 changes: 1 addition & 1 deletion tests/Api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('sendRequest()', async () => {
headers.reduce((carry, item) => {
carry[item[0]] = item[1]
return carry
}, {})
}, {}),
)
})

Expand Down
15 changes: 14 additions & 1 deletion tests/resources/Guest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('validateCode()', async () => {
expect(apiMock).toHaveBeenCalledTimes(1)
expect(apiMock).toHaveBeenCalledWith(
'/events/event-uuid/guests/validate-code',
{ method: 'post', body: '{"code":"guest-code"}' }
{ method: 'post', body: '{"code":"guest-code"}' },
)
})

Expand All @@ -36,6 +36,19 @@ test('create()', async () => {
})
})

test('createCompanion()', async () => {
guest.createCompanion('guest-code', { a: 'b' })

expect(apiMock).toHaveBeenCalledTimes(1)
expect(apiMock).toHaveBeenCalledWith(
'/events/event-uuid/guests/guest-code/companions',
{
method: 'post',
body: '{"a":"b"}',
},
)
})

test('update()', async () => {
guest.update('guest-code', { a: 'b' })

Expand Down
Loading

0 comments on commit b9056fe

Please sign in to comment.