Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
feat: orders metrics endpoints added (#602)
Browse files Browse the repository at this point in the history
* feat: orders metrics endpoints added

* fix: console.log removed
  • Loading branch information
butikden authored Mar 23, 2022
1 parent c60f3d9 commit 88996aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/endpoints/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BaseExtend from '../extends/base'
import { formatQueryParams } from '../utils/helpers'

class MetricsEndpoint extends BaseExtend {
constructor(endpoint) {
super(endpoint)

this.endpoint = 'metrics'
}

TotalOrders(query) {
const formattedString = formatQueryParams(query)
return this.request.send(
`${this.endpoint}/orders/total?${formattedString}`,
'GET'
)
}

TotalValue(query) {
const formattedString = formatQueryParams(query)
return this.request.send(
`${this.endpoint}/orders/value?${formattedString}`,
'GET'
)
}
}

export default MetricsEndpoint
6 changes: 4 additions & 2 deletions src/moltin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { PCMVariationsEndpoint } from "./types/pcm-variations"
import { PcmVariationsRelationshipsEndpoint } from "./types/pcm-variations-relationships"
import {UserAuthenticationPasswordProfileEndpoint
} from "./types/user-authentication-password-profile";
import { MetricsEndpoint } from './types/metrics'

export * from './types/config'
export * from './types/storage'
Expand Down Expand Up @@ -94,7 +95,7 @@ export * from './types/account-members'
export * from './types/account-memberships'
export * from './types/pcm-variations'
export * from './types/pcm-variations-relationships'

export * from './types/metrics'

// UMD
export as namespace moltin
Expand Down Expand Up @@ -143,7 +144,8 @@ export class Moltin {
PasswordProfile: PasswordProfileEndpoint
UserAuthenticationPasswordProfile: UserAuthenticationPasswordProfileEndpoint;
PCMVariations: PCMVariationsEndpoint;
PcmVariationsRelationship: PcmVariationsRelationshipsEndpoint
PcmVariationsRelationship: PcmVariationsRelationshipsEndpoint;
Metrics: MetricsEndpoint

Cart(id?: string): CartEndpoint // This optional cart id is super worrying when using the SDK in a node server :/
constructor(config: Config)
Expand Down
2 changes: 2 additions & 0 deletions src/moltin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import AccountMembersEndpoint from './endpoints/account-members'
import AccountAuthenticationSettingsEndpoint from './endpoints/account-authentication-settings'
import AccountMembershipsEndpoint from './endpoints/account-memberships'
import PCMVariationsEndpoint from './endpoints/pcm-variations'
import MetricsEndpoint from './endpoints/metrics'

import { cartIdentifier, tokenInvalid, getCredentials } from './utils/helpers'
import CatalogsEndpoint from './endpoints/catalogs'
Expand Down Expand Up @@ -99,6 +100,7 @@ export default class Moltin {
this.UserAuthenticationPasswordProfile = new UserAuthenticationPasswordProfileEndpoint(
config
)
this.Metrics = new MetricsEndpoint(config)
}

// Expose `Cart` class on Moltin class
Expand Down
19 changes: 19 additions & 0 deletions src/types/metrics.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ResourcePage } from './core'

export interface MetricsBase {
value: number
time: string
}

export interface MetricsQuery {
currency?: string
from: string
to: string
interval: string
}

export interface MetricsEndpoint {
TotalOrders(query: MetricsQuery): Promise<ResourcePage<MetricsBase>>

TotalValue(query: MetricsQuery): Promise<ResourcePage<MetricsBase>>
}
6 changes: 6 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ function buildQueryParams({ includes, sort, limit, offset, filter }) {
.join('&')
}

export function formatQueryParams(query) {
return Object.keys(query)
.map(k => formatQueryString(k, query[k]))
.join('&')
}

export function buildURL(endpoint, params) {
if (
params.includes ||
Expand Down

0 comments on commit 88996aa

Please sign in to comment.