Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for new recipe management #4

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Typescript ADaaS Library (@devrev/ts-adaas) provides:

## Release Notes

#### v0.0.3

- Support for new recipe management

#### v0.0.2

- Support for the State API
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/ts-adaas",
"version": "0.0.2",
"version": "0.0.3",
"description": "Typescript library containing the ADaaS(AirDrop as a Service) control protocol.",
"type": "commonjs",
"main": "./dist/src/index.js",
Expand Down
93 changes: 93 additions & 0 deletions src/common/install-initial-domain-mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import axios from 'axios';
import { AirdropEvent } from '../types/extraction';
import { InitialDomainMapping } from '../types/common';

export async function installInitialDomainMapping(
event: AirdropEvent,
initialDomainMappingJson: InitialDomainMapping
) {
const devrevEndpoint = event.execution_metadata.devrev_endpoint;
const devrevToken = event.context.secrets.service_account_token;
const snapInVersionId = event.context.snap_in_version_id;

if (!initialDomainMappingJson) {
console.warn('No initial domain mapping found');
return;
}

const snapInVersionResponse = await axios.get(
devrevEndpoint + '/internal/snap-in-versions.get',
{
headers: {
Authorization: devrevToken,
},
params: {
id: snapInVersionId,
},
}
);

const importSlug = snapInVersionResponse.data.snap_in_version.imports[0].slug;
const snapInSlug = snapInVersionResponse.data.snap_in_version.slug;
const startingRecipeBlueprint =
initialDomainMappingJson?.starting_recipe_blueprint;

let recipeBlueprintId;
if (
startingRecipeBlueprint &&
Object.keys(startingRecipeBlueprint).length !== 0
) {
try {
const recipeBlueprintResponse = await axios.post(
`${devrevEndpoint}/internal/airdrop.recipe.blueprints.create`,
{
...startingRecipeBlueprint,
},
{
headers: {
Authorization: devrevToken,
},
}
);

recipeBlueprintId = recipeBlueprintResponse.data.recipe_blueprint.id;

console.log(
'Successfully created recipe blueprint with id: ' + recipeBlueprintId
);
} catch (error) {
console.error('Error while creating recipe blueprint', error);
}
}

try {
// 2. Install the initial domain mappings
const additionalMappings =
initialDomainMappingJson.additional_mappings || {};
const initialDomainMappingInstallResponse = await axios.post(
`${devrevEndpoint}/internal/airdrop.recipe.initial-domain-mappings.install`,
{
external_system_type: 'ADaaS',
import_slug: importSlug,
snap_in_slug: snapInSlug,
...(recipeBlueprintId && {
starting_recipe_blueprint: recipeBlueprintId,
}),
...additionalMappings,
},
{
headers: {
Authorization: devrevToken,
},
}
);

console.log(
'Successfully installed initial domain mapping',
initialDomainMappingInstallResponse.data
);
} catch (error) {
console.error('Error while installing initial domain mapping', error);
return;
}
}
38 changes: 38 additions & 0 deletions src/demo-extractor/external_domain_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"record_types": {
"users": {
"fields": {
"name": {
"is_required": true,
"type": "text",
"name": "Name",
"text": {
"min_length": 1
}
},
"email": {
"type": "text",
"name": "Email",
"is_required": true
}
}
},
"contacts": {
"fields": {
"name": {
"is_required": true,
"type": "text",
"name": "Name",
"text": {
"min_length": 1
}
},
"email": {
"type": "text",
"name": "Email",
"is_required": true
}
}
}
}
}
80 changes: 35 additions & 45 deletions src/demo-extractor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Adapter } from '../adapter';
import { Uploader } from '../uploader';

import extractorInitialDomainMapping from './initial_domain_mapping.json';
import externalDomainMetadata from './external_domain_metadata.json';

type ExtractorState = object;

Expand Down Expand Up @@ -48,21 +48,10 @@ export class DemoExtractor {
}

case EventType.ExtractionMetadataStart: {
const metadata = [
{
item: 'contacts',
fields: ['id', 'name', 'lastName'],
},
{
item: 'users',
fields: ['id', 'name', 'lastName'],
},
];

const { artifact, error } = await this.uploader.upload(
'loopback_metadata_1.jsonl',
'metadata',
metadata
'metadata_1.jsonl',
'external_domain_metadata',
externalDomainMetadata
);

if (error || !artifact) {
Expand All @@ -72,23 +61,8 @@ export class DemoExtractor {
return;
}

const { artifact: recipe, error: recipeError } =
await this.uploader.upload(
'recipe.json',
'initial_domain_mapping',
extractorInitialDomainMapping
);

if (recipeError || !recipe) {
await this.adapter.emit(ExtractorEventType.ExtractionMetadataError, {
error: recipeError,
});
return;
}

await this.adapter.emit(ExtractorEventType.ExtractionMetadataDone, {
progress: 50,
artifacts: [artifact, recipe],
artifacts: [artifact],
});

break;
Expand All @@ -97,19 +71,27 @@ export class DemoExtractor {
case EventType.ExtractionDataStart: {
const contacts = [
{
id: 1,
name: 'John',
lastName: 'Doe',
id: 'contact-1',
created_date: '1999-12-25T01:00:03+01:00',
modified_date: '1999-12-25T01:00:03+01:00',
data: {
email: 'johnsmith@test.com',
name: 'John Smith',
},
},
{
id: 2,
name: 'Jane',
lastName: 'Doe',
id: 'contact-2',
created_date: '1999-12-27T15:31:34+01:00',
modified_date: '2002-04-09T01:55:31+02:00',
data: {
email: 'janesmith@test.com',
name: 'Jane Smith',
},
},
];

const { artifact, error } = await this.uploader.upload(
'loopback_contacts_1.json',
'contacts_1.json',
'contacts',
contacts
);
Expand All @@ -133,19 +115,27 @@ export class DemoExtractor {
case EventType.ExtractionDataContinue: {
const users = [
{
id: 1,
name: 'John',
lastName: 'Phd',
id: 'user-1',
created_date: '1999-12-25T01:00:03+01:00',
modified_date: '1999-12-25T01:00:03+01:00',
data: {
email: 'johndoe@test.com',
name: 'John Doe',
},
},
{
id: 2,
name: 'Jane',
lastName: 'Phd',
id: 'user-2',
created_date: '1999-12-27T15:31:34+01:00',
modified_date: '2002-04-09T01:55:31+02:00',
data: {
email: 'janedoe@test.com',
name: 'Jane Doe',
},
},
];

const { artifact, error } = await this.uploader.upload(
'loopback_users_1.json',
'users_1.json',
'users',
users
);
Expand Down
107 changes: 0 additions & 107 deletions src/demo-extractor/initial_domain_mapping.json

This file was deleted.

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './demo-extractor';
export * from './uploader';
export * from './types';
export * from './http';

export * from './common/install-initial-domain-mapping';
Loading
Loading