Skip to content

Instantly share code, notes, and snippets.

@maatthc
Last active July 15, 2020 06:12
Show Gist options
  • Save maatthc/6e5c00d665571c7bf06d686c8f20c1e7 to your computer and use it in GitHub Desktop.
Save maatthc/6e5c00d665571c7bf06d686c8f20c1e7 to your computer and use it in GitHub Desktop.
OrderDynamics Helper Class - WIP
import { soap } from "strong-soap"
abstract class OrderDynamicsClient {
protected isAuthenticated: Boolean = false
protected soapClient: any
protected url: any
abstract authenticate(): Promise<void>
createClient(options: any) {
return new Promise<any>((resolve, reject) => {
soap.createClient(this.url, options, (err: any, client: any) => {
if (err) {
reject(err);
} else {
resolve(client);
}
})
})
}
}
export class OrderDynamicsAuthenticator extends OrderDynamicsClient {
private authToken: String = ''
constructor(readonly url: string, readonly storeId: String, readonly accessKey: String) {
super()
}
public async authenticate(): Promise<void> {
this.soapClient = await this.createClient({})
const { result } = await this.soapClient.Authenticate({
storeId: this.storeId,
accessKey: this.accessKey
})
this.authToken = result.AuthenticateResult.Result
this.isAuthenticated = true
}
public async getToken(): Promise<String> {
if (!this.isAuthenticated) await this.authenticate()
return this.authToken
}
}
interface OdRequest {
token: String,
siteId: Number,
historyMinutes: Number
}
// The idea is to have this class as a generic wrapper for any query methods - WIP
export class OrderDynamicsQueryExecuter extends OrderDynamicsClient {
private historyMinutes: number = 10
private request: OdRequest
constructor(readonly url: string, private token: String, readonly siteId: Number) {
super()
this.request = { token, siteId, historyMinutes: this.historyMinutes }
}
public async authenticate(): Promise<void> {
this.soapClient = await this.createClient({})
this.isAuthenticated = true
}
public async getShipments(): Promise<void> {
if (!this.isAuthenticated) await this.authenticate()
const { result } = await this.soapClient.GetLastUpdatedOrderShipmentsECP(this.request)
return result.GetLastUpdatedOrderShipmentsECPResult.Result.OrderShipmentShortInfo
}
}
// How to use
const init = async () => {
//TODO : fill the gaps
const SecretKey = ''
const StoreId = ''
const odServiceUrl = ''
const odAuthUrl = ''
const jurisdiction = {
au: 2,
nz: 3
}
const odAuth = new OrderDynamicsAuthenticator(odAuthUrl, StoreId, SecretKey)
const odExecuter = new OrderDynamicsQueryExecuter(odServiceUrl, await odAuth.getToken(), jurisdiction.au)
console.log(await odExecuter.getShipments())
}
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment