From a0ebbd614eca69a74a7a245e90fc859e18b1f0c2 Mon Sep 17 00:00:00 2001 From: Fi1osof Date: Sat, 24 Nov 2018 08:25:27 +0300 Subject: [PATCH] Import projects --- .gitignore | 1 + __tests__/module/schema/index.test.mjs | 34 ++- package.json | 11 +- src/modules/processor/index.mjs | 237 +++++++++++++++--- .../database/cooperation/project.graphql | 3 +- .../database/cooperation/service.graphql | 2 +- src/modules/schema/database/resource.graphql | 2 +- 7 files changed, 248 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index d199c6b..cdb2f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ /lib /node_modules /umd +/uploads *.log* *.lock diff --git a/__tests__/module/schema/index.test.mjs b/__tests__/module/schema/index.test.mjs index 8619a78..ea509d7 100644 --- a/__tests__/module/schema/index.test.mjs +++ b/__tests__/module/schema/index.test.mjs @@ -83,6 +83,9 @@ const requiredTypes = [ "Tags", "class_key", "template", + "Project", + "Image", + // "Gallery", ], prisma: [ ], @@ -110,7 +113,7 @@ const requiredTypes = [ both: [ "id", "oldID", - "image", + "Image", "Resource", ], prisma: [ @@ -119,6 +122,21 @@ const requiredTypes = [ ], }, }, + { + name: "ProjectMember", + fields: { + both: [ + "id", + "User", + "Project", + "Services", + ], + prisma: [ + ], + api: [ + ], + }, + }, { name: "Service", fields: { @@ -126,6 +144,20 @@ const requiredTypes = [ "id", "oldID", "Resource", + "Projects", + ], + prisma: [ + ], + api: [ + ], + }, + }, + { + name: "File", + fields: { + both: [ + "id", + "ImageResource", ], prisma: [ ], diff --git a/package.json b/package.json index 5e32985..fa1f473 100644 --- a/package.json +++ b/package.json @@ -14,21 +14,24 @@ "test": "NODE_OPTIONS=\"--experimental-modules\" yarn mocha --delay __tests__/" }, "dependencies": { + "@prisma-cms/cooperation-module": "latest", "@prisma-cms/log-module": "latest", "@prisma-cms/prisma-module": "latest", - "@prisma-cms/user-module": "latest", - "@prisma-cms/resource-module": "latest", "@prisma-cms/prisma-processor": "latest", + "@prisma-cms/resource-module": "latest", "@prisma-cms/society-module": "latest", - "@prisma-cms/cooperation-module": "latest", + "@prisma-cms/user-module": "latest", "chalk": "^2.4.1", "draft-js": "^0.10.5", "jsdom": "^13.0.0", "knex": "^0.15.2", "merge-graphql-schemas": "latest", + "mime": "^2.3.1", "mysql": "^2.16.0", + "punycode": "^2.1.1", "react": "^16.6.1", - "react-dom": "^16.6.1" + "react-dom": "^16.6.1", + "urijs": "^1.19.1" }, "devDependencies": { "@prisma-cms/server": "latest", diff --git a/src/modules/processor/index.mjs b/src/modules/processor/index.mjs index 67a1756..c7b68e0 100644 --- a/src/modules/processor/index.mjs +++ b/src/modules/processor/index.mjs @@ -16,6 +16,13 @@ import Draft from "draft-js"; import jsdom from 'jsdom'; +import fs from "fs"; +import path from "path"; +import mime from "mime"; + +// import URI from "urijs"; +import punycode from "punycode"; + const { JSDOM } = jsdom; @@ -281,8 +288,8 @@ export default class ImportProcessor extends PrismaProcessor { // await this.importTags(); // await this.importNotificationTypes(); - await this.importServices(); - // await this.importProjects(); + // await this.importServices(); + await this.importProjects(); // await this.importVotes(); @@ -1568,7 +1575,7 @@ export default class ImportProcessor extends PrismaProcessor { let updatedAt = editedon ? new Date(editedon * 1000) : undefined; - const query = target.getQuery("Resource") + const query = target.getQuery("Service") await query.update({ createdAt, @@ -1612,22 +1619,29 @@ export default class ImportProcessor extends PrismaProcessor { ; query - .leftJoin(target.getTableName("Resource", "target"), { + .leftJoin(target.getTableName("Project", "target"), { "target.oldID": "source.id", - "target.template": 29, + // "target.template": 29, }) .innerJoin(target.getTableName("User"), "User.oldID", "source.createdby") + .leftJoin(source.getTableName("site_tmplvar_contentvalues", "tv_image"), function () { + this + .on("tv_image.tmplvarid", 3) + .on("tv_image.contentid", "source.id") + .on(knex.raw(`tv_image.value > ''`)) + ; + }) .where("source.parent", 1443) .whereNull("target.id") ; - query.select([ "source.*", "User.id as createdById", + "tv_image.value as image", ]); - query.limit(1); + // query.limit(1); // console.log(chalk.green("query SQL"), query.toString()); @@ -1640,7 +1654,7 @@ export default class ImportProcessor extends PrismaProcessor { await this.log(`Было получено ${objects && objects.length} проектов`, "Info"); - return; + // return; const processor = this.getProcessor(objects, this.writeProject.bind(this)); @@ -1655,12 +1669,8 @@ export default class ImportProcessor extends PrismaProcessor { async writeProject(object) { - // console.log(chalk.green("writeProject object"), object); - // throw new Error("writeTopic error test"); - const { ctx, - source, target, } = this @@ -1670,39 +1680,159 @@ export default class ImportProcessor extends PrismaProcessor { let result; - const { + let { id: oldID, - type: name, - comment, + pagetitle: name, + longtitle, + createdon, + editedon, + createdby, + uri, + published, + deleted, + hidemenu, + searchable, + content: text, + class_key, + template, + image, } = object; + let type = "Project"; - // Получаем пользователей с этим уведомлением + let { + content, + contentText, + } = this.getContent(text) || {}; + uri = this.prepareUri(uri); - const query = source.getQuery("society_notice_users", "source") + name = name.trim().replace(/^\/|\/$/g, ''); + + let url = name; + + if(!url.match(/^http.*?:\/\//)){ + url = `http://${url}`; + } + + + name = punycode.toUnicode(name); + + name = name.replace(/^http.*?:\/\//, ''); + + /** + * Получаем всех участников проекта + */ + + const { + source, + } = this; + + + const knex = source.getKnex(); + + const membersQuery = source.getQuery("modxsite_projects_members", "source") ; - query + membersQuery + .where("source.project_id", oldID) .innerJoin(target.getTableName("User"), "User.oldID", "source.user_id") - .where("notice_id", oldID) + .innerJoin(target.getTableName("Service"), "Service.oldID", "source.service_id") + // .whereNull("target.id") ; - query.select([ - "User.id as userId", + + membersQuery.select([ + // "source.*", + "source.project_id", + "source.user_id", + knex.raw("GROUP_CONCAT(service_id) as services"), + // "User.id as createdById", ]); - // query.limit(3); + membersQuery.groupBy(1); + membersQuery.groupBy(2); - // console.log(chalk.green("query SQL"), query.toString()); + // query.limit(1); - const users = await query.then(); + // console.log(chalk.green("membersQuery SQL"), membersQuery.toString()); + + // throw new Error ("Topic error test"); + + const objects = await membersQuery.then(); // console.log("objects", objects); - await this.log(`Было получено ${users && users.length} пользователей-уведомлений`, "Info"); + await this.log(`Было получено ${objects && objects.length} участников проекта`, "Info"); + + // return; + let Members = { + create: objects.map(n => { + + const { + status, + user_id, + services, + } = n; + + return { + status: status === 1 ? "Active" : "Invited", + User: { + connect: { + oldID: user_id, + }, + }, + Services: { + connect: services.split(",").map(service_id => { + return { + oldID: parseInt(service_id), + }; + }), + }, + CreatedBy: { + connect: { + oldID: createdby, + }, + }, + } + }), + } + + + let Image; + + if (image && fs.existsSync(`uploads/${image}`)) { + + // console.log("image", image); + + const filename = path.basename(image); + // // console.log("image.basename", path.basename(image)); + // // console.log("image.format", path.format(image)); + // // console.log("image.extname", path.extname(image)); + + // console.log("mime", mime.getType(image)); + + const mimetype = mime.getType(image); + + Image = { + create: { + path: image, + filename, + mimetype, + encoding: "7bit", + CreatedBy: { + connect: { + oldID: createdby, + }, + }, + }, + } + + } + + // return; /** * Сохраняем объект @@ -1711,22 +1841,61 @@ export default class ImportProcessor extends PrismaProcessor { data: { oldID, name, - comment, + url, CreatedBy: { connect: { - username: "Fi1osof", + oldID: createdby, }, }, - Users: users && users.length ? { - connect: users.map(({ userId }) => { - return { - id: userId, - } - }), - } : undefined, + Resource: { + create: { + type, + uri, + name, + longtitle, + class_key, + template, + content, + contentText, + published: published === 1, + deleted: deleted === 1, + hidemenu: hidemenu === 1, + searchable: searchable === 1, + Image, + CreatedBy: { + connect: { + oldID: createdby, + }, + }, + }, + }, + Members, }, }); + const { + id: objectId, + } = result; + + /** + * Если пользователь был сохранен, надо обновить дату его создания + */ + let createdAt = createdon ? new Date(createdon * 1000) : undefined; + let updatedAt = editedon ? new Date(editedon * 1000) : undefined; + + + const query = target.getQuery("Project") + + await query.update({ + createdAt, + updatedAt, + }) + .where({ + id: objectId, + }) + .then(); + + // console.log(chalk.green("update query SQL"), query.toString()); return result; } diff --git a/src/modules/schema/database/cooperation/project.graphql b/src/modules/schema/database/cooperation/project.graphql index e8dead5..05c7ae5 100644 --- a/src/modules/schema/database/cooperation/project.graphql +++ b/src/modules/schema/database/cooperation/project.graphql @@ -2,6 +2,7 @@ type Project{ oldID: Int - image: File + Image: File Resource: Resource @relation(name: "ProjectResource") + url: String } diff --git a/src/modules/schema/database/cooperation/service.graphql b/src/modules/schema/database/cooperation/service.graphql index f554334..818cc2f 100644 --- a/src/modules/schema/database/cooperation/service.graphql +++ b/src/modules/schema/database/cooperation/service.graphql @@ -1,6 +1,6 @@ type Service{ - oldID: Int + oldID: Int @unique Resource: Resource! @relation(name: "ServiceResource") } diff --git a/src/modules/schema/database/resource.graphql b/src/modules/schema/database/resource.graphql index d5a9f3b..16c27eb 100644 --- a/src/modules/schema/database/resource.graphql +++ b/src/modules/schema/database/resource.graphql @@ -5,11 +5,11 @@ type Resource { Topics: [Resource!]! @relation(name: "BlogTopics") Blog: Resource @relation(name: "BlogTopics") Service: Service @relation(name: "ServiceResource") + Project: Project @relation(name: "ProjectResource") """Будет удалено""" class_key: String """Будет удалено""" template: Int - Projects: [Project!]! @relation(name: "ProjectResource") } \ No newline at end of file