123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- const express = require('express');
- const path = require('path');
- const RepositorySystem = require('../systems/RepositorySystem');
- const Actions = require('../systems/ActionSystem');
- class ManyToMany {
- constructor(name, inceptor){
- this.name = name;
- this.scheme = {
- auto_increment: 2000,
- fields: [
- {
- key: "name",
- type: "string",
- keyLength: 200
- }
- ]
- };
- this.repository = RepositorySystem.create(this.name, this.scheme);
- this.inceptor = inceptor;
- this.inceptorScheme = {
- fields: [
- {
- key: name + "_ids",
- type: "text"
- }
- ]
- };
- this.inceptorRepo = RepositorySystem.create(this.inceptor, this.inceptorScheme);
- this._init2();
- }
- _init2() {
- Actions.on('model'+this.inceptor, (model) => {
- return this.repository.getPage(0,1000)
- .then((categories) => {
- return {
- ...model,
- [this.name + "_ids"]: {
- disabled: true
- },
- [this.name]: {
- Filter: {
- type: "manytomany",
- options: categories.reduce((data,a) => {return {...data, [a.id]: a.name }}, {}),
- name: this.name,
- field: this.name + '_ids',
- order: 60
- }
- }
- }
- })
- });
- Actions.on('get'+this.inceptor, (product) => {
- if (!product[this.name + "_ids"])return product;
- let cids = product[this.name + "_ids"].split('|');
- return this.repository.raw().whereIn('id',cids)
- .then((categories) => {
- let dict = categories.map((i) => i.name);
- product[this.name] = dict;
- delete product[this.name + "_ids"];
- return product;
- });
- });
- Actions.on('import'+this.inceptor, (dataset) => {
- return this.repository.getPage(0,1000).then((cats) => {
- let catNames = cats.reduce((w, a) => {
- w[a.name] = a.id;
- return w;
- },{});
- let categories = dataset.reduce((acc, item) => {
- let categories = (item[this.name] || '').split(',');
- for(var i in categories) {
- if(!acc.includes(categories[i]))acc.push(categories[i])
- }
- return acc;
- },[]);
- var filtered = categories.filter((value, index, arr) => {
- return !catNames[value];
- });
- console.log("FILTERED", filtered);
- filtered = filtered.map((name) => {return {name}})
- return this.repository.batchInsert(filtered)
- .then(() => this.repository.getPage(0,1000))
- .then((cats) => {
- return cats.reduce((w, a) => {
- w[a.name] = a.id;
- return w;
- },{});
- })
- .then((names) => dataset.map((item) => {
- let categories = (item[this.name] || '').split(',');
- let res = [];
- for(var i in categories) {
- res.push(names[categories[i]])
- }
- if(res.length)
- item[this.name + "_ids"] = res.join('|');
- return item;
- }));
- });
- });
- Actions.on('post'+this.inceptor, (product) => {
- if(!product[this.name + "_ids"])
- return product;
- let cats = product[this.name + "_ids"];
- return this.repository.raw().whereIn('id', cats)
- .then((categories) => {
- let cids = categories.map((c) => c.id);
- if(cids.length)
- product[this.name + "_ids"] = cids.join('|');
- return product;
- });
- });
- }
- }
- module.exports = ManyToMany;
|