Skip to content

Commit

Permalink
adding the option to add upstream ids to event tabels in xlsx templat…
Browse files Browse the repository at this point in the history
…e (as relatedEntity with some role)
  • Loading branch information
johannesliem committed Aug 29, 2023
1 parent 4eeda7a commit 5cd19e3
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-otters-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@intavia/data-import": patch
---

adding the option to add upstream ids to event tabels in xlsx template (as relatedEntity with some role)
85 changes: 83 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@intavia/api-client": "^0.1.25",
"buffer": "^6.0.3",
"lodash.isequal": "^4.5.0",
"xlsx": "^0.18.5"
},
Expand Down
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const eventTargetProps: Array<string> = [
"source",
"startDate",
"endDate",
"relations",
];

export const biographyTargetProps: Array<string> = ["id", "text", "citation"];
Expand Down
42 changes: 34 additions & 8 deletions src/create-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import type {
import { eventTargetProps } from "./config";
import { eventPropertyMappers } from "./data-mappers";
import type { UnmappedProps, VocabularyIdAndEntry } from "./types";
import { Buffer } from "buffer";

interface CreateEventReturn {
event: Event;
vocabularyEntries?: Array<VocabularyIdAndEntry>;
upstreamEntities?: Array<Entity["id"]>;
unmappedProperties?: UnmappedProps;
}

Expand All @@ -28,6 +30,7 @@ export function createEvent(entry: Record<string, unknown>): CreateEventReturn {

const event: any = {};
const vocabularyEntries: Array<VocabularyIdAndEntry> = [];
const upstreamEntities: Array<Entity["id"]> = [];

for (const targetProp of targetProps) {
if (!(targetProp in eventPropertyMappers)) {
Expand Down Expand Up @@ -65,6 +68,19 @@ export function createEvent(entry: Record<string, unknown>): CreateEventReturn {
}
}

// eventEntityRelation
const {
eventEntityRelation,
vocabularyEntries: _vocabularyEntries,
upstreamEntities: _upstreamEntities,
} = createEventEntityRelation(entry);
if (!("relations" in event)) {
event.relations = [];
}
eventEntityRelation !== undefined && event.relations.push(eventEntityRelation);
_vocabularyEntries !== undefined && vocabularyEntries.push(..._vocabularyEntries);
_upstreamEntities !== undefined && upstreamEntities.push(..._upstreamEntities);

// has Place ? add additional EventEntityRelation
if (entry.place != null && String(entry.place).trim().length > 0) {
for (const place of String(entry.place).split(";")) {
Expand Down Expand Up @@ -97,13 +113,15 @@ export function createEvent(entry: Record<string, unknown>): CreateEventReturn {
return {
event,
vocabularyEntries,
upstreamEntities,
unmappedProperties,
};
}

interface CreateEventEntityRelationReturn {
eventEntityRelation?: EventEntityRelation;
vocabularyEntries?: Array<VocabularyIdAndEntry>;
upstreamEntities?: Array<Entity["id"]>;
error?: string;
}

Expand All @@ -121,14 +139,22 @@ export function createEventEntityRelation(
return { error: "some error" };
}

if (mapper.vocabulary !== undefined) {
return {
eventEntityRelation: mapper.mapper(entry)[0],
vocabularyEntries: [mapper.vocabulary(entry)],
};
const eventEntityRelation = mapper.mapper(entry)[0];
const base64candidate = eventEntityRelation.entity.split("-")[1];
const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
const isUpstream =
base64regex.test(base64candidate) &&
Buffer.from(base64candidate, "base64").toString("utf-8").startsWith("http");

const result: CreateEventEntityRelationReturn = {};

if (isUpstream) {
eventEntityRelation.entity = base64candidate;
result.upstreamEntities = [eventEntityRelation.entity];
}

return {
eventEntityRelation: mapper.mapper(entry)[0],
};
eventEntityRelation && (result.eventEntityRelation = eventEntityRelation);
mapper.vocabulary !== undefined && (result.vocabularyEntries = [mapper.vocabulary(entry)]);

return result;
}
1 change: 1 addition & 0 deletions src/import-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ImportData {
unmappedEntries?: Array<unknown>;
collections?: Record<string, CollectionCandidate>;
tags?: Array<TagCandidate>;
upstreamEntityIds?: Array<Entity["id"]>;
}

interface ImportDataParams {
Expand Down
21 changes: 19 additions & 2 deletions src/transform-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function transformData(params: TransformDataParams): ImportData {
const entityGroups: Record<string, Array<Entity["id"]>> = {};
const collections: ImportData["collections"] = {};
const tags: ImportData["tags"] = [];
const upstreamEntityIds: ImportData["upstreamEntityIds"] = [];

const registerVocabularyEntries = (
vocabularyEntries: Array<VocabularyIdAndEntry> | undefined
Expand All @@ -44,6 +45,18 @@ export function transformData(params: TransformDataParams): ImportData {
}
};

const registerUpstreamEntities = (
_upstreamEntities: ImportData["upstreamEntityIds"] | undefined
) => {
if (_upstreamEntities !== undefined) {
for (const upstreamEntity of _upstreamEntities) {
if (!upstreamEntityIds.includes(upstreamEntity)) {
upstreamEntityIds.push(upstreamEntity);
}
}
}
};

// check if any of the entries has tagging
// has Tag for all? otherwise use prefix for all entities and events
let tagsForAll = input.filter((entry) => {
Expand Down Expand Up @@ -129,22 +142,25 @@ export function transformData(params: TransformDataParams): ImportData {
// Add relation to existing event (rows in the xlsx that have the same id an only add relations; other props are ignored)
events = events.map((event) => {
if (event.id === entry.id) {
const { eventEntityRelation, vocabularyEntries } =
const { eventEntityRelation, vocabularyEntries, upstreamEntities } =
createEventEntityRelation(entry);

eventEntityRelation !== undefined &&
event.relations.push(eventEntityRelation);
registerVocabularyEntries(vocabularyEntries);
registerUpstreamEntities(upstreamEntities);

// TODO unmapped entries
}
return event;
});
} else {
// Create new event
const { event, vocabularyEntries, unmappedProperties } = createEvent(entry);
const { event, vocabularyEntries, upstreamEntities, unmappedProperties } =
createEvent(entry);
events.push(event);
registerVocabularyEntries(vocabularyEntries);
registerUpstreamEntities(upstreamEntities);

const entryGroup = entry.sheetName as string;
if (!(entryGroup in eventGroups)) {
Expand Down Expand Up @@ -272,6 +288,7 @@ export function transformData(params: TransformDataParams): ImportData {
unmappedEntries && (result.unmappedEntries = unmappedEntries);
collections && (result.collections = collections);
tags && (result.tags = _tags);
upstreamEntityIds && (result.upstreamEntityIds = upstreamEntityIds);

return result;
}

0 comments on commit 5cd19e3

Please sign in to comment.