Skip to content

Commit

Permalink
feat: (wip) add sync logic for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinsandilya committed Apr 1, 2024
1 parent 936095a commit 7e3fe23
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 36 deletions.
1 change: 1 addition & 0 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@usevenice/connector-toggl": "workspace:*",
"@usevenice/connector-venmo": "workspace:*",
"@usevenice/connector-wise": "workspace:*",
"@usevenice/connector-xero": "workspace:*",
"@usevenice/connector-yodlee": "workspace:*",
"@usevenice/engine-backend": "workspace:*",
"@usevenice/env": "workspace:*",
Expand Down
36 changes: 24 additions & 12 deletions connectors/connector-xero/def.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import type {
ConnectorDef,
ConnectorSchemas,
EntityPayloadWithRaw,
} from '@usevenice/cdk'
import {connHelpers} from '@usevenice/cdk'
import {z, zCast} from '@usevenice/util'
import type {components} from '@opensdks/sdk-xero/xero_accounting.oas.types'
import type {ConnectorDef, ConnectorSchemas} from '@usevenice/cdk'
import {connHelpers, oauthBaseSchema, zEntityPayload} from '@usevenice/cdk'
import {R, z} from '@usevenice/util'

export const zConfig = oauthBaseSchema.connectorConfig

const oReso = oauthBaseSchema.resourceSettings
export const zSettings = oReso.extend({
oauth: oReso.shape.oauth,
})

export type XERO = components['schemas']

export const XERO_ENTITY_NAME = {
Accounts: 'Accounts',
PurchaseOrders: 'PurchaseOrders',
}

export const xeroSchemas = {
name: z.literal('xero'),
resourceSettings: z.object({
access_token: z.string(),
}),
destinationInputEntity: zCast<EntityPayloadWithRaw>(),
connectorConfig: zConfig,
resourceSettings: zSettings,
connectOutput: oauthBaseSchema.connectOutput,
sourceOutputEntity: zEntityPayload,
sourceOutputEntities: R.mapValues(XERO_ENTITY_NAME, () => z.unknown()),
} satisfies ConnectorSchemas

export const helpers = connHelpers(xeroSchemas)
Expand All @@ -21,7 +33,7 @@ export const xeroDef = {
categories: ['accounting'],
logoUrl: '/_assets/logo-xero.svg',
displayName: 'Xero',
stage: 'alpha',
stage: 'beta',
nangoProvider: 'xero',
},
name: 'xero',
Expand Down
3 changes: 2 additions & 1 deletion connectors/connector-xero/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"sideEffects": [],
"module": "./index.ts",
"dependencies": {
"@opensdks/sdk-xero": "^0.0.2",
"@opensdks/runtime": "^0.0.16",
"@opensdks/sdk-xero": "^0.0.5",
"@opensdks/util-zod": "^0.0.15",
"@usevenice/cdk": "workspace:*",
"@usevenice/util": "workspace:*"
Expand Down
47 changes: 42 additions & 5 deletions connectors/connector-xero/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import {initXeroSDK} from '@opensdks/sdk-xero'
import type {ConnectorServer} from '@usevenice/cdk'
import type {xeroSchemas} from './def'
import {nangoProxyLink} from '@usevenice/cdk'
import {rxjs} from '@usevenice/util'
import {type xeroSchemas} from './def'

export const xeroServer = {
newInstance: ({settings}) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
newInstance: ({settings, fetchLinks}) => {
const xero = initXeroSDK({
headers: {authorization: `Bearer ${settings.access_token}`},
headers: {
authorization: `Bearer ${settings.oauth.credentials.access_token}`,
},
links: (defaultLinks) => [
(req, next) => {
if (xero.clientOptions.baseUrl) {
req.headers.set(
nangoProxyLink.kBaseUrlOverride,
xero.clientOptions.baseUrl,
)
}
return next(req)
},
...fetchLinks,
...defaultLinks,
],
})
// TODO(@jatin): Add logic here to handle sync.
return xero
},
sourceSync: ({instance: xero}) => {
// TODO(@jatin): Add logic here to handle sync.
// TODO: ensure instance has typesafety for some reason the types are not present here :/.
console.log('[xero] Starting sync', xero)
const getAll = async () => {
const result = await xero.client.GET('/Accounts', {
params: {
header: {
'xero-tenant-id': '35325d8f-5087-4c6d-b413-c3f740c26f2e', // TODO: Remove hardcoding
},
},
})
console.log('result', result)
return result.data?.Accounts
}

getAll()
.then((res) => console.log('[data test]', res))
.catch((err) => console.log('error', err))

return rxjs.empty()
},
} satisfies ConnectorServer<typeof xeroSchemas>

export default xeroServer
23 changes: 11 additions & 12 deletions packages/engine-backend/router/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,19 @@ export const trpc = initTRPC
})

export const publicProcedure = trpc.procedure
// Enable me for logs
// .use(
// ({next, ctx, input, rawInput, meta, path}) => {
// console.log('[trpc]', {
// input,
// rawInput,
// meta,
// path,
// })
// return next({ctx})
// },
// )
// Enable me for logs
.use(({next, ctx, input, rawInput, meta, path}) => {
console.log('[trpc]', {
input,
rawInput,
meta,
path,
})
return next({ctx})
})

export const protectedProcedure = publicProcedure.use(({next, ctx}) => {
console.log('DEBUG', ctx.viewer)
if (!hasRole(ctx.viewer, ['end_user', 'user', 'org', 'system'])) {
throw new TRPCError({
code: ctx.viewer.role === 'anon' ? 'UNAUTHORIZED' : 'FORBIDDEN',
Expand Down
6 changes: 5 additions & 1 deletion packages/engine-backend/router/resourceRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ export const resourceRouter = trpc.router({
return
}
const reso = await ctx.asOrgIfNeeded.getResourceExpandedOrFail(resoId)
console.log('[syncResource]', reso, opts)
console.log(
'[syncResource]',
opts?.metaOnly,
reso.connectorConfig.connector.sourceSync,
)
// No need to checkResource here as sourceSync should take care of it

if (opts?.metaOnly) {
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-frontend/VeniceConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function _VeniceConnect({
}) {
const nangoPublicKey =
_trpcReact.getPublicEnv.useQuery().data?.NEXT_PUBLIC_NANGO_PUBLIC_KEY

console.log('DEBUG', connectorConfigInfos, clientConnectors)
const nangoFrontend = React.useMemo(
() =>
nangoPublicKey &&
Expand Down
29 changes: 25 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e3fe23

Please sign in to comment.