-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcf9598
commit bd41808
Showing
10 changed files
with
345 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,6 @@ errors: | |
BadRequestError: | ||
status-code: 400 | ||
type: BaseError | ||
NotImplementedError: | ||
status-code: 500 | ||
type: BaseError |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json | ||
|
||
imports: | ||
errors: common/errors.yml | ||
types: common/types.yml | ||
|
||
types: | ||
TriggerSyncResponse: | ||
properties: | ||
status: types.ResponseStatus | ||
|
||
service: | ||
base-path: /sync | ||
auth: false | ||
audiences: | ||
- external | ||
endpoints: | ||
triggerSync: | ||
docs: Trigger sync for a specific tenant | ||
method: POST | ||
path: '' | ||
request: | ||
name: TriggerSyncRequest | ||
headers: | ||
x-revert-api-token: | ||
type: string | ||
docs: Your official API key for accessing revert apis. | ||
x-revert-t-id: | ||
type: string | ||
docs: The unique customer id used when the customer linked their account. | ||
x-connection-api-key: | ||
type: optional<string> | ||
docs: API key for third party provider | ||
response: TriggerSyncResponse | ||
errors: | ||
- errors.UnAuthorizedError | ||
- errors.InternalServerError | ||
- errors.NotFoundError | ||
- errors.NotImplementedError |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Connection } from '../../generated/typescript/api/resources/common/index.js'; | ||
import config from '../../config'; | ||
const { initSDK } = require('@opensdks/runtime'); | ||
const { veniceSdkDef } = require('@opensdks/sdk-venice'); | ||
|
||
const syncTwentyConnection = async (connection: Connection, connectionAPIKey: string) => { | ||
const connectionId = connection.t_id; | ||
const openIntApiKey = config.OPEN_INT_API_KEY; | ||
if (!config.OPEN_INT_API_KEY || !connectionAPIKey) { | ||
console.log('Credentials absent to make this sync happen for: ', connection.t_id, ' returning early'); | ||
return; | ||
} | ||
const venice = initSDK( | ||
{ | ||
...veniceSdkDef, | ||
oasMeta: { | ||
...veniceSdkDef.oasMeta, | ||
// servers: [{ url: 'http://localhost:4000/api/v0' }], | ||
}, | ||
}, | ||
{ | ||
headers: { | ||
'x-apikey': openIntApiKey, | ||
}, | ||
} | ||
); | ||
|
||
const twentyAccessToken = connectionAPIKey; | ||
|
||
const connectorConfig = await venice.GET('/core/connector_config'); | ||
|
||
const getConnectorConfig = (cName: string, connectorConfig: any) => | ||
connectorConfig?.data?.filter((c: any) => c.connectorName === cName)[0]; | ||
|
||
const twenty = getConnectorConfig('twenty', connectorConfig); | ||
const revert = getConnectorConfig('revert', connectorConfig); | ||
|
||
let revertResource = await venice.GET('/core/resource', { | ||
params: { | ||
query: { | ||
connectorConfigId: revert.id, | ||
endUserId: connectionId, | ||
}, | ||
}, | ||
}); | ||
|
||
let twentyResource = await venice.GET('/core/resource', { | ||
params: { | ||
query: { | ||
connectorConfigId: twenty.id, | ||
endUserId: connectionId, | ||
}, | ||
}, | ||
}); | ||
|
||
let twentyResourceId = twentyResource?.data[0]?.id; | ||
let revertResourceId = revertResource?.data[0]?.id; | ||
|
||
twentyResource.data = twentyResource?.data[0]; | ||
revertResource.data = revertResource?.data[0]; | ||
|
||
// Create a twenty resource for this connection | ||
if (!twentyResourceId) { | ||
twentyResourceId = await venice.POST('/core/resource', { | ||
body: { | ||
connectorName: twenty.connectorName, | ||
displayName: null, | ||
endUserId: connectionId, | ||
connectorConfigId: twenty.id, | ||
integrationId: null, | ||
settings: { access_token: twentyAccessToken }, | ||
}, | ||
}); | ||
|
||
console.log('Created twenty resourceId', twentyResourceId.data); | ||
|
||
twentyResource = await venice.GET(`/core/resource/${twentyResourceId.data}`); | ||
|
||
console.log('Created twenty resource', twentyResource); | ||
} | ||
|
||
// Create a revert resource for this connection in this environment of Twenty if not created in Twenty’s organisation, | ||
if (!revertResourceId) { | ||
revertResourceId = await venice.POST('/core/resource', { | ||
body: { | ||
connectorName: revert.connectorName, | ||
displayName: null, | ||
endUserId: connectionId, | ||
connectorConfigId: revert.id, | ||
integrationId: null, | ||
settings: { tenant_id: connectionId }, | ||
}, | ||
}); | ||
console.log('Created revert resourceId', revertResourceId.data); | ||
revertResource = await venice.GET(`/core/resource/${revertResourceId.data}`); | ||
|
||
console.log('Created revert resource', revertResource); | ||
} | ||
|
||
console.log('REVERT RESOURCE', revertResourceId); | ||
|
||
console.log('TWENTY RESOURCE', twentyResourceId); | ||
|
||
// Create a pipeline between them if not created, | ||
// Trigger this created pipeline | ||
const syncPipeline = await venice.GET('/core/pipeline', { | ||
params: { | ||
query: { | ||
resourceIds: [twentyResource?.data, revertResource?.data], | ||
}, | ||
}, | ||
}); | ||
|
||
if (!syncPipeline?.data[0]) { | ||
const createdPipeline = await venice.POST('/core/pipeline', { | ||
body: { | ||
id: `pipe_${connectionId}`, | ||
sourceId: revertResource?.data.id, | ||
destinationId: twentyResource?.data.id, | ||
}, | ||
}); | ||
syncPipeline.data = [createdPipeline.data]; | ||
console.log('Created pipeline', syncPipeline?.data); | ||
} | ||
|
||
console.log('PIPELINE', syncPipeline?.data[0]); | ||
|
||
void venice | ||
.POST(`/core/pipeline/${syncPipeline?.data[0]?.id}/_sync`, { | ||
params: { | ||
async: true, | ||
}, | ||
}) | ||
.then() | ||
.catch((e: Error) => console.log('Error', e)) | ||
.finally(() => { | ||
console.log('TRIGGERED'); | ||
}); | ||
}; | ||
|
||
export { syncTwentyConnection }; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import config from '../config'; | ||
import { Connection, NotImplementedError } from '../generated/typescript/api/resources/common'; | ||
import { SyncService } from '../generated/typescript/api/resources/sync/service/SyncService'; | ||
import revertAuthMiddleware from '../helpers/authMiddleware'; | ||
import revertTenantMiddleware from '../helpers/tenantIdMiddleware'; | ||
import { syncTwentyConnection } from './custom/twenty'; | ||
|
||
const syncService = new SyncService( | ||
{ | ||
async triggerSync(req, res) { | ||
const account = res.locals.account; | ||
if (account.id === config.TWENTY_ACCOUNT_ID) { | ||
const { 'x-connection-api-key': twentyAPIKey } = req.headers; | ||
const connection = res.locals.connection as Connection; | ||
await syncTwentyConnection(connection, twentyAPIKey as string); | ||
res.send({ | ||
status: 'ok', | ||
}); | ||
} else | ||
throw new NotImplementedError({ | ||
error: 'Syncing is not yet available for your account, sorry! Contact us on discord or email to get access!', | ||
}); | ||
}, | ||
}, | ||
[revertAuthMiddleware(), revertTenantMiddleware()] | ||
); | ||
|
||
export { syncService }; |
Oops, something went wrong.