Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Commit

Permalink
Import Topics
Browse files Browse the repository at this point in the history
  • Loading branch information
Fi1osof committed Nov 11, 2018
1 parent 090e442 commit 533cd93
Showing 1 changed file with 167 additions and 12 deletions.
179 changes: 167 additions & 12 deletions src/modules/processor/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ export default class ImportProcessor extends PrismaProcessor {
}


createLog(args) {

const {
id: importId,
} = this.Import || {};

if (importId) {
Object.assign(args.data, {
Import: {
connect: {
id: importId,
},
},
});
}

return super.createLog(args);
}


async initDB(args) {

// console.log("initDB args", args);
Expand Down Expand Up @@ -169,8 +189,8 @@ export default class ImportProcessor extends PrismaProcessor {
await this.initDB(args);

// await this.importUsers();
await this.importBlogs();
// await this.importTopics();
// await this.importBlogs();
await this.importTopics();
// await this.importComments();

}
Expand Down Expand Up @@ -456,26 +476,161 @@ export default class ImportProcessor extends PrismaProcessor {
*/


/**
* Import Topics
*/
async importTopics() {

createLog(args) {
this.log("Импортируем топики", "Info");

// throw new Error("Test");

const {
id: importId,
} = this.Import || {};
source,
target,
ctx,
} = this;

if (importId) {
Object.assign(args.data, {
Import: {


const query = source.getQuery("site_content", "source")
;

query
.leftJoin(target.getTableName("Resource", "target"), "target.oldID", "source.id")
.innerJoin(target.getTableName("User"), "User.oldID", "source.createdby")
.whereNull("target.id")
.whereIn("template", [
15,
])
;

query
.leftJoin(source.getTableName("society_blog_topic", "blog_topic"), "blog_topic.topicid", "source.id")

query
.leftJoin(target.getTableName("Resource", "Blog"), "Blog.oldID", "blog_topic.blogid")


query.select([
"source.*",
"Blog.id as blogId",
]);

query.limit(1);


// console.log(chalk.green("query SQL"), query.toString());


const objects = await query.then();

// console.log("objects", objects);

await this.log(`Было получено ${objects && objects.length} топиков`, "Info");

// return;

const processor = this.getProcessor(objects, this.writeTopic.bind(this));

for await (const result of processor) {

// console.log("writeUser result", result);

}

}


async writeTopic(object) {

const {
ctx,
target,
} = this

const {
db,
} = ctx;

let result;

const {
id,
pagetitle: name,
createdon,
editedon,
createdby,
uri,
template,
published,
deleted,
hidemenu,
searchable,
blogId,
} = object;

let type = "Topic";

/**
* Сохраняем объект
*/
result = await db.mutation.createResource({
data: {
type,
oldID: id,
uri,
name,
published: published === 1,
deleted: deleted === 1,
hidemenu: hidemenu === 1,
searchable: searchable === 1,
CreatedBy: {
connect: {
id: importId,
oldID: createdby,
},
},
});
}
Blog: blogId ? {
connect: {
id: blogId,
},
} : undefined,
},
});

return super.createLog(args);
const {
id: objectId,
} = result;

/**
* Если пользователь был сохранен, надо обновить дату его создания
*/
let createdAt = createdon ? new Date(createdon * 1000) : undefined;
let updatedAt = editedon ? new Date(editedon * 1000) : undefined;


const query = target.getQuery("Resource")

await query.update({
createdAt,
updatedAt,
})
.where({
id: objectId,
})
.then();

// console.log(chalk.green("update query SQL"), query.toString());

return result;
}

/**
* Eof Import Topics
*/




}

0 comments on commit 533cd93

Please sign in to comment.