Skip to content

Commit

Permalink
feat(taxes): cover api
Browse files Browse the repository at this point in the history
  • Loading branch information
eljefedelrodeodeljefe committed Jan 3, 2019
1 parent 68b17a3 commit 3d347ca
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ export class TaxesFetchFailed extends BaseError {
}
}

export class TaxesPutFailed extends BaseError {
public name = 'TaxesPutFailed'
constructor(public message: string = 'Could not alter tax', properties?: any) {
super(message, properties)
}
}

export class TaxesCreationFailed extends BaseError {
public name = 'TaxesCreationFailed'
constructor(public message: string = 'Could not create tax', properties?: any) {
super(message, properties)
}
}

export class ProductsCreateFailed extends BaseError {
public name = 'ProductsCreateFailed'
constructor(public message: string = 'Could not create the product', properties?: any) {
Expand Down
2 changes: 1 addition & 1 deletion src/v0/product_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ProductGroups {

return resolve({
data: response.data.results,
metadata: {},
metadata: { count: response.data.count },
next
} as ProductGroupsResponse)
} catch (error) {
Expand Down
88 changes: 83 additions & 5 deletions src/v0/taxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,32 @@ export interface TaxesQuery {
}

export interface TaxesResponse {
data: object[]
data: Tax[]
metadata: object
}

export interface TaxResponse {
data: Tax
metadata?: {
count?: number
patch?: any
}
msg?: string
}
export interface Tax {
id?: string
}

export interface Tax {
name: string
fa_account_number?: string
type: 'vat'
account: string
rate?: string
percentage?: string
is_fixed: boolean
}

export class Taxes {
endpoint: string
http: Client
Expand All @@ -31,6 +53,8 @@ export class Taxes {

getAll(query?: TaxesQuery | undefined): Promise<TaxesResponse> {
return new Promise(async (resolve, reject) => {
let next

try {
let uri
if (query && query.uri) {
Expand All @@ -40,14 +64,68 @@ export class Taxes {
}

const response = await this.http.getClient().get(uri)
response.status !== 200 && reject(new errors.TaxesFetchFailed())
if (response.status !== 200) {
return reject(new errors.TaxesFetchFailed(undefined, { status: response.status }))
}

return resolve({
data: response.data.results,
metadata: { count: response.data.count }
metadata: { count: response.data.count },
next
} as TaxesResponse)
} catch (err) {
return reject(new errors.TaxesFetchFailed())
} catch (error) {
return reject(new errors.TaxesFetchFailed(undefined, { error }))
}
})
}

get(taxId: string): Promise<TaxResponse> {
return new Promise(async (resolve, reject) => {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}/${taxId}`
try {
const response = await this.http.getClient().get(uri)
response.status !== 200 &&
reject(new errors.TaxesFetchFailed(undefined, { status: response.status }))

return resolve({
data: response.data.results[0] as Tax,
msg: response.data.msg,
metadata: { count: response.data.count }
} as TaxResponse)
} catch (error) {
return reject(new errors.TaxesFetchFailed(undefined, { error }))
}
})
}

put(taxId: string, tax: Tax): Promise<TaxResponse> {
return new Promise(async (resolve, reject) => {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}/${taxId}`
try {
const response = await this.http.getClient().put(uri, tax)

return resolve({
data: response.data.results[0] as Tax,
metadata: { count: response.data.count }
} as TaxResponse)
} catch (error) {
return reject(new errors.TaxesPutFailed(undefined, { error }))
}
})
}

create(tax: Tax): Promise<TaxResponse> {
return new Promise(async (resolve, reject) => {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}`
try {
const response = await this.http.getClient().post(uri, tax)

return resolve({
data: response.data.results[0] as Tax,
metadata: { count: response.data.count }
} as TaxResponse)
} catch (error) {
return reject(new errors.TaxesCreationFailed(undefined, { error }))
}
})
}
Expand Down

0 comments on commit 3d347ca

Please sign in to comment.