Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kiryakov <[email protected]>
  • Loading branch information
Stepan-Kirjakov committed Jul 28, 2023
1 parent d3280c9 commit 24dcb2b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 54 deletions.
4 changes: 4 additions & 0 deletions common/src/hedera-modules/message/message-body.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ export interface ModuleMessageBody extends MessageBody {
* Owner
*/
owner: string;
/**
* Topic id
*/
topicId: string;
/**
* CID
*/
Expand Down
10 changes: 10 additions & 0 deletions common/src/hedera-modules/message/module-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class ModuleMessage extends Message {
* Owner
*/
public owner: string;
/**
* Module topic ID
* @private
*/
public moduleTopicId: string;

constructor(type: MessageType.Module, action: MessageAction) {
super(action, type);
Expand All @@ -49,6 +54,7 @@ export class ModuleMessage extends Message {
this.description = model.description;
this.owner = model.owner;
this.document = zip;
this.moduleTopicId = model.topicId;
}

/**
Expand All @@ -72,6 +78,7 @@ export class ModuleMessage extends Message {
name: this.name,
description: this.description,
owner: this.owner,
topicId: this.moduleTopicId?.toString(),
cid: this.getDocumentUrl(UrlType.cid),
uri: this.getDocumentUrl(UrlType.url)
}
Expand Down Expand Up @@ -131,6 +138,8 @@ export class ModuleMessage extends Message {
message.uuid = json.uuid;
message.name = json.name;
message.description = json.description;
message.owner = json.owner;
message.moduleTopicId = json.topicId;

if (json.cid) {
const urls = [{
Expand Down Expand Up @@ -177,6 +186,7 @@ export class ModuleMessage extends Message {
result.description = this.description;
result.owner = this.owner;
result.document = this.document;
result.moduleTopicId = this.moduleTopicId;
return result;
}

Expand Down
65 changes: 39 additions & 26 deletions guardian-service/src/api/helpers/schema-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,52 @@ import { readJSON } from 'fs-extra';
import { DatabaseServer, MessageAction, MessageServer, Schema as SchemaCollection, SchemaConverterUtils, SchemaMessage, TopicConfig, TopicHelper, Users, } from '@guardian/common';
import { INotifier } from '@helpers/notifier';

/**
* Import Result
*/
export interface SchemaImportResult {
/**
* Old schema id
*/
oldID: string,
/**
* New schema id
*/
newID: string,
/**
* Old schema uuid
*/
oldUUID: string,
/**
* New schema uuid
*/
newUUID: string,
/**
* Old schema iri
*/
oldIRI: string,
/**
* New schema iri
*/
newIRI: string,
/**
* Old schema message id
*/
oldMessageID: string
/**
* Old schema message id
*/
newMessageID: string
}

/**
* Import Result
*/
export interface ImportResult {
/**
* New schema uuid
*/
schemasMap: {
/**
* Old schema id
*/
oldID: string,
/**
* New schema id
*/
newID: string,
/**
* Old schema uuid
*/
oldUUID: string,
/**
* New schema uuid
*/
newUUID: string,
/**
* Old schema iri
*/
oldIRI: string,
/**
* New schema iri
*/
newIRI: string,
}[];
schemasMap: SchemaImportResult[];
/**
* Errors
*/
Expand Down
43 changes: 38 additions & 5 deletions guardian-service/src/api/helpers/schema-import-export-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GenerateUUIDv4, ISchema, ModelHelper, Schema, SchemaCategory, SchemaEntity, SchemaHelper, SchemaStatus } from '@guardian/interfaces';
import { DatabaseServer, Logger, MessageAction, MessageServer, MessageType, replaceValueRecursive, SchemaConverterUtils, SchemaMessage, UrlType } from '@guardian/common';
import { DatabaseServer, Logger, MessageAction, MessageServer, MessageType, replaceValueRecursive, SchemaConverterUtils, SchemaMessage, Tag, TagMessage, UrlType } from '@guardian/common';
import { emptyNotifier, INotifier } from '@helpers/notifier';
import { importTag } from './../tag.service';
import { createSchema, fixSchemaDefsOnImport, getDefs, ImportResult, onlyUnique } from './schema-helper';
import { createSchema, fixSchemaDefsOnImport, getDefs, ImportResult, onlyUnique, SchemaImportResult } from './schema-helper';

export const schemaCache = {};

Expand Down Expand Up @@ -60,13 +60,14 @@ export async function loadSchema(messageId: string, owner: string) {
*/
export async function importTagsByFiles(
result: ImportResult,
files: any[],
files: Tag[],
notifier: INotifier
): Promise<ImportResult> {
const { schemasMap } = result;
const idMap: Map<string, string> = new Map();
for (const item of schemasMap) {
idMap.set(item.oldID, item.newID);
idMap.set(item.oldMessageID, item.newID);
}
await importTag(files, idMap);
return result;
Expand Down Expand Up @@ -114,7 +115,7 @@ export async function importSchemaByFiles(
): Promise<ImportResult> {
notifier.start('Import schemas');

const schemasMap: any[] = [];
const schemasMap: SchemaImportResult[] = [];
const uuidMap: Map<string, string> = new Map();

for (const file of files) {
Expand All @@ -126,7 +127,9 @@ export async function importSchemaByFiles(
oldUUID,
newUUID,
oldIRI: `#${oldUUID}`,
newIRI: `#${newUUID}`
newIRI: `#${newUUID}`,
oldMessageID: file.messageId,
newMessageID: null,
})
if (oldUUID) {
uuidMap.set(oldUUID, newUUID);
Expand Down Expand Up @@ -206,7 +209,37 @@ export async function importSchemasByMessages(
}

notifier.start('Load tags');
const topics = new Set<string>();
for (const schema of schemas) {
topics.add(schema.topicId);
}

const tags: any[] = [];
const messageServer = new MessageServer(null, null);
for (const id of topics) {
const tagMessages = await messageServer.getMessages<TagMessage>(
id,
MessageType.Tag,
MessageAction.PublishTag
);
for (const tag of tagMessages) {
tags.push({
uuid: tag.uuid,
name: tag.name,
description: tag.description,
owner: tag.owner,
entity: tag.entity,
target: tag.target,
status: 'History',
topicId: tag.topicId,
messageId: tag.id,
document: null,
uri: null,
date: tag.date,
id: null
});
}
}

notifier.completed();

Expand Down
48 changes: 37 additions & 11 deletions guardian-service/src/api/module.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiResponse } from '@api/helpers/api-response';
import { BinaryMessageResponse, DataBaseHelper, DatabaseServer, Logger, MessageAction, MessageError, MessageResponse, MessageServer, MessageType, ModuleMessage, PolicyModule, Schema, TopicConfig, TopicHelper, Users, } from '@guardian/common';
import { BinaryMessageResponse, DataBaseHelper, DatabaseServer, Logger, MessageAction, MessageError, MessageResponse, MessageServer, MessageType, ModuleMessage, PolicyModule, Schema, TagMessage, TopicConfig, TopicHelper, Users, } from '@guardian/common';
import { GenerateUUIDv4, MessageAPI, ModuleStatus, SchemaCategory, TagType, TopicType } from '@guardian/interfaces';
import JSZip from 'jszip';
import { emptyNotifier, INotifier } from '@helpers/notifier';
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function generateZipFile(module: PolicyModule): Promise<JSZip> {
zip.folder('schemas');
for (const schema of schemas) {
tagTargets.push(schema.id.toString());
const item = {...schema};
const item = { ...schema };
delete item._id;
delete item.id;
delete item.status;
Expand Down Expand Up @@ -82,10 +82,10 @@ export async function parseZipFile(zipFile: any): Promise<any> {
.map(file => file[1].async('string')));

const module = JSON.parse(moduleString);
const tags = tagsStringArray.map(item => JSON.parse(item));
const tags = tagsStringArray.map(item => JSON.parse(item)) || [];
const schemas = schemasStringArray.map(item => JSON.parse(item));

return {module, tags, schemas};
return { module, tags, schemas };
}

/**
Expand Down Expand Up @@ -114,6 +114,8 @@ export async function preparePreviewMessage(messageId: string, owner: string, no

notifier.completedAndStart('Parse module files');
const result = await parseZipFile(message.document);
result.messageId = messageId;
result.moduleTopicId = message.moduleTopicId;

notifier.completed();
return result;
Expand Down Expand Up @@ -194,7 +196,6 @@ export async function publishModule(model: PolicyModule, owner: string, notifier
await DatabaseServer.saveTopic(rootTopic.toObject());

model.topicId = rootTopic.topicId;
model.status = ModuleStatus.PUBLISHED;

notifier.completedAndStart('Generate file');

Expand All @@ -213,6 +214,7 @@ export async function publishModule(model: PolicyModule, owner: string, notifier
const result = await messageServer
.sendMessage(message);
model.messageId = result.getId();
model.status = ModuleStatus.PUBLISHED;

notifier.completedAndStart('Link topic and module');
await topicHelper.twoWayLink(rootTopic, userTopic, result.getId());
Expand Down Expand Up @@ -317,7 +319,7 @@ export async function modulesAPI(): Promise<void> {
if (item.config?.variables) {
for (const variable of item.config.variables) {
if (variable.baseSchema) {
variable.baseSchema = await DatabaseServer.getSchema({iri: variable.baseSchema});
variable.baseSchema = await DatabaseServer.getSchema({ iri: variable.baseSchema });
}
}
}
Expand Down Expand Up @@ -455,7 +457,7 @@ export async function modulesAPI(): Promise<void> {

const preview = await parseZipFile(Buffer.from(zip.data));

const {module, tags, schemas} = preview;
const { module, tags, schemas } = preview;
delete module._id;
delete module.id;
delete module.messageId;
Expand All @@ -465,7 +467,7 @@ export async function modulesAPI(): Promise<void> {
module.owner = owner;
module.status = 'DRAFT';
module.type = 'CUSTOM';
if (await DatabaseServer.getModule({name: module.name})) {
if (await DatabaseServer.getModule({ name: module.name })) {
module.name = module.name + '_' + Date.now();
}
const item = await DatabaseServer.createModules(module);
Expand Down Expand Up @@ -500,7 +502,7 @@ export async function modulesAPI(): Promise<void> {
const notifier = emptyNotifier();
const preview = await preparePreviewMessage(messageId, owner, notifier);

const { module, tags } = preview;
const { module, tags, moduleTopicId } = preview;
delete module._id;
delete module.id;
delete module.messageId;
Expand All @@ -515,11 +517,35 @@ export async function modulesAPI(): Promise<void> {
}
const item = await DatabaseServer.createModules(module);

if (Array.isArray(tags)) {
const messageServer = new MessageServer(null, null);
const tagMessages = await messageServer.getMessages<TagMessage>(
moduleTopicId,
MessageType.Tag,
MessageAction.PublishTag
);
for (const tag of tagMessages) {
if (tag.entity === TagType.Module && tag.target === messageId) {
tags.push({
uuid: tag.uuid,
name: tag.name,
description: tag.description,
owner: tag.owner,
entity: tag.entity,
target: tag.target,
status: 'History',
topicId: tag.topicId,
messageId: tag.id,
date: tag.date,
document: null,
uri: null,
id: null
});
}
}
if (tags.length) {
const moduleTags = tags.filter((t: any) => t.entity === TagType.Module);
await importTag(moduleTags, item.id.toString());
}

return new MessageResponse(item);
} catch (error) {
new Logger().error(error, ['GUARDIAN_SERVICE']);
Expand Down
Loading

0 comments on commit 24dcb2b

Please sign in to comment.