Created
March 10, 2020 12:04
-
-
Save dmytro-kerest/90ad39a3176a8f96e5f0327348bb5fea to your computer and use it in GitHub Desktop.
Simple backend servce to create project in Xero API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Config from '../config/config'; | |
| import { AccountingAPIClient as XeroClient } from 'xero-node'; | |
| import { XeroClientConfiguration } from 'xero-node/lib/internals/BaseAPIClient'; | |
| export default class XeroService { | |
| static createProject = async (name: string) => { | |
| const { apiConfig, trackingProjectCategoryName } = Config.xero; | |
| try { | |
| let xeroClient = new XeroClient(apiConfig as XeroClientConfiguration); | |
| const response = await xeroClient.trackingCategories.get(); | |
| const trackingCaterory = response.TrackingCategories.find(tc => tc.Name === trackingProjectCategoryName); | |
| const result:any = await xeroClient.trackingCategories.trackingOptions.create( | |
| { Name: name }, | |
| { TrackingCategoryID: trackingCaterory.TrackingCategoryID } | |
| ); | |
| return result.Options; | |
| } catch(e) { | |
| console.log(e); | |
| throw new Error(`Error Creating Project <${name}> in Xero. Mesage From Xero: ${e.message}`); | |
| } | |
| } | |
| static removeProject = async (name: string) => { | |
| const { apiConfig, trackingProjectCategoryName } = Config.xero; | |
| let xeroClient = new XeroClient(apiConfig as XeroClientConfiguration); | |
| const response = await xeroClient.trackingCategories.get(); | |
| const trackingCaterory = response.TrackingCategories.find(tc => tc.Name === trackingProjectCategoryName); | |
| const option = trackingCaterory.Options.find(opt => opt.Name === name); | |
| if(option && option.TrackingOptionID) { | |
| const result:any = await xeroClient.trackingCategories.trackingOptions.delete({ | |
| TrackingCategoryID: trackingCaterory.TrackingCategoryID, | |
| TrackingOptionID: option.TrackingOptionID | |
| }); | |
| return result; | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment