@digione/node-custom-api 0.2.0-alpha1 → 0.2.0-alpha2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digione/node-custom-api",
3
- "version": "0.2.0-alpha1",
3
+ "version": "0.2.0-alpha2",
4
4
  "description": "Typescript node digione-api",
5
5
  "author": "Monchai Jirayupong <monchai.j@seven.co.th>",
6
6
  "license": "MIT",
package/utils/file.d.ts CHANGED
@@ -62,16 +62,23 @@ export declare class FileUtil {
62
62
  width?: number;
63
63
  height?: number;
64
64
  }): Promise<any>;
65
- beforeFindFile(where?: {}, { tags, group }?: {
65
+ beforeFindFile(where?: {}, { attributes, tags, group, verify }?: {
66
+ attributes?: any;
66
67
  tags?: TagFile;
67
68
  group?: any;
68
- }): {};
69
- findAllFile({ where, folder, full, file_link, thumb, width, height, parent_folder, attributes, limit, offset, tag, tags, group }?: {
69
+ verify?: boolean;
70
+ }): {
71
+ where: {};
72
+ attributes: any;
73
+ order: any[];
74
+ };
75
+ findAllFile({ where, folder, full, file_link, thumb, verify, width, height, parent_folder, attributes, limit, offset, tag, tags, group }?: {
70
76
  where?: {};
71
77
  folder?: boolean;
72
78
  full?: boolean;
73
79
  file_link?: boolean;
74
80
  thumb?: boolean;
81
+ verify?: boolean;
75
82
  width?: number;
76
83
  height?: number;
77
84
  parent_folder?: string;
package/utils/file.js CHANGED
@@ -108,6 +108,7 @@ class FileUtil {
108
108
  }
109
109
  afterQueryFile(data = {}, { tag = {}, file_link = false } = {}) {
110
110
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
111
+ delete data['thumbnail'];
111
112
  data['custom'] = data['custom'] || {};
112
113
  data['tags'] = data['tags'] || [];
113
114
  if (data['filename'] == data['path']) {
@@ -276,12 +277,12 @@ class FileUtil {
276
277
  return null;
277
278
  where['$folder.parent_id$'] = query['id'];
278
279
  }
279
- where = this.beforeFindFile(where, { tags, group });
280
+ let option = this.beforeFindFile(where, { attributes: ['id', 'extension', 'filename', 'path', 'thumb'], tags, group });
280
281
  let result = yield file_1.FileModel.schema(this.ref, "_").findOne({
281
- raw: true, attributes: ['id', 'extension', 'filename', 'path', 'thumb'],
282
- where: Object.assign({ '$folder.slug$': slug }, where),
282
+ raw: true, attributes: option.attributes,
283
+ where: Object.assign({ '$folder.slug$': slug }, option.where),
283
284
  include: [{ as: 'folder', model: folder_1.FolderModel.schema(this.ref, "_"), attributes: [] }],
284
- order: [['sort', 'asc'], ['tags', 'desc']]
285
+ order: option.order
285
286
  });
286
287
  return this.findImageThumb(result, { width, height });
287
288
  }
@@ -299,7 +300,8 @@ class FileUtil {
299
300
  return this.findImageThumb(item, { width, height });
300
301
  });
301
302
  }
302
- beforeFindFile(where = {}, { tags = [], group = undefined } = {}) {
303
+ beforeFindFile(where = {}, { attributes = undefined, tags = [], group = undefined, verify = false } = {}) {
304
+ let order = [['sort', 'asc'], ['tags', 'desc']], thumbnail = false;
303
305
  if (this.option.organ_id) {
304
306
  where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ [sequelize_1.Op.or]: [{ organ_id: this.option.organ_id }, { organ_by: this.option.organ_id }] });
305
307
  }
@@ -310,9 +312,11 @@ class FileUtil {
310
312
  let op = tags.reduce((total, tag) => {
311
313
  let value = Object.keys(tag).reduce((total, key) => total.concat([`'${key}'`, `"${tag[key]}"`]), []).join(',');
312
314
  if (value) {
313
- total = total.concat((0, sequelize_1.literal)(`JSON_CONTAINS(tags,JSON_OBJECT(${value}))`));
314
- if (tag['slug'] == "thumbnail") {
315
- total = total.concat((0, sequelize_1.literal)(`tags IS NULL`));
315
+ if (tag['slug'] == "thumbnail" && !verify) {
316
+ thumbnail = true;
317
+ }
318
+ else {
319
+ total = total.concat((0, sequelize_1.literal)(`JSON_CONTAINS(tags,JSON_OBJECT(${value}))`));
316
320
  }
317
321
  }
318
322
  return total;
@@ -321,9 +325,19 @@ class FileUtil {
321
325
  where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ [sequelize_1.Op.or]: op });
322
326
  }
323
327
  }
324
- return where;
328
+ if (thumbnail) {
329
+ let include = [[(0, sequelize_1.literal)(`IF(JSON_CONTAINS(tags,JSON_OBJECT('slug',"thumbnail")),'yes','no')`), 'thumbnail']];
330
+ if (typeof attributes == "undefined") {
331
+ attributes = { include };
332
+ }
333
+ else if (attributes instanceof Array) {
334
+ attributes = attributes.concat(include);
335
+ }
336
+ order = [['thumbnail', 'desc'], ['sort', 'asc']];
337
+ }
338
+ return { where, attributes, order };
325
339
  }
326
- findAllFile({ where = {}, folder = false, full = false, file_link = false, thumb = false, width = 0, height = 0, parent_folder = "", attributes = undefined, limit = undefined, offset = undefined, tag = {}, tags = [], group = undefined } = {}) {
340
+ findAllFile({ where = {}, folder = false, full = false, file_link = false, thumb = false, verify = true, width = 0, height = 0, parent_folder = "", attributes = undefined, limit = undefined, offset = undefined, tag = {}, tags = [], group = undefined } = {}) {
327
341
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
328
342
  try {
329
343
  let include = [];
@@ -335,10 +349,10 @@ class FileUtil {
335
349
  return [];
336
350
  where['$folder.parent_id$'] = query['id'];
337
351
  }
338
- where = this.beforeFindFile(where, { tags, group });
339
352
  where['is_hidden'] = where['is_hidden'] || "no";
353
+ let option = this.beforeFindFile(where, { attributes, tags, group, verify });
340
354
  let result = yield file_1.FileModel.schema(this.ref, "_").findAll({
341
- attributes, raw: true, where, limit, offset, include, order: [['sort', 'asc'], ['tags', 'desc']]
355
+ attributes: option.attributes, raw: true, where: option.where, limit, offset, include, order: option.order
342
356
  });
343
357
  result.length && (yield this.getStorage());
344
358
  result = yield Promise.all(result.map((item) => tslib_1.__awaiter(this, void 0, void 0, function* () {
@@ -372,9 +386,9 @@ class FileUtil {
372
386
  if (!folder)
373
387
  return { rows: [], count: 0 };
374
388
  where['folder_id'] = folder['id'];
375
- where = this.beforeFindFile(where, { tags, group });
376
389
  where['is_hidden'] = where['is_hidden'] || "no";
377
- let data = yield Promise.all([this.findAllFile({ where, full: true, file_link, limit, offset, tag, group }), file_1.FileModel.schema(this.ref, "_").count({ where })]);
390
+ let option = this.beforeFindFile(where, { tags, group, verify: true });
391
+ let data = yield Promise.all([this.findAllFile({ where, full: true, file_link, limit, offset, tag, group }), file_1.FileModel.schema(this.ref, "_").count({ where: option.where })]);
378
392
  return { rows: data[0], count: data[1] };
379
393
  });
380
394
  }
@@ -385,7 +399,7 @@ class FileUtil {
385
399
  }
386
400
  findOneFileByFolder(slug, { where = {}, full = false, file_link = false, parent_folder = "", tags = [], group = null } = {}) {
387
401
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
388
- return (yield this.findAllFile({ where: Object.assign({ '$folder.slug$': slug }, where), folder: true, full, file_link, parent_folder, limit: 1, tags, group }))[0] || null;
402
+ return (yield this.findAllFile({ where: Object.assign({ '$folder.slug$': slug }, where), folder: true, full, file_link, verify: false, parent_folder, limit: 1, tags, group }))[0] || null;
389
403
  });
390
404
  }
391
405
  findOneFileById(id, { where = {}, full = true, file_link = false, parent_folder = "", tags = [], group = null } = {}) {
@@ -678,8 +692,8 @@ class FileUtil {
678
692
  }
679
693
  remove({ where = {}, delete_object = false, error = true, tags = [], group = undefined } = {}) {
680
694
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
681
- where = this.beforeFindFile(where, { tags, group });
682
- let files = yield file_1.FileModel.schema(this.ref, "_").findAll({ where, attributes: ['id', 'path', 'thumb'], raw: true });
695
+ let option = this.beforeFindFile(where, { tags, group, verify: true });
696
+ let files = yield file_1.FileModel.schema(this.ref, "_").findAll({ where: option.where, attributes: ['id', 'path', 'thumb'], raw: true });
683
697
  if (files.length) {
684
698
  let paths = [], id = files.map(i => { paths = paths.concat(this.getFilePath(i)); return i['id']; });
685
699
  yield file_1.FileModel.schema(this.ref, "_").destroy({ where: { id } });