new(basic.gblib): New COPY keyword working.

This commit is contained in:
Rodrigo Rodriguez 2021-01-15 08:46:28 -03:00
parent 2a46d6d0dd
commit d106158ba6

View file

@ -31,6 +31,7 @@
\*****************************************************************************/ \*****************************************************************************/
'use strict'; 'use strict';
import { GBLog, GBMinInstance } from 'botlib'; import { GBLog, GBMinInstance } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework';
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
import urlJoin = require('url-join'); import urlJoin = require('url-join');
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService'; import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
@ -38,14 +39,15 @@ import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
import { SecService } from '../../security.gbapp/services/SecService'; import { SecService } from '../../security.gbapp/services/SecService';
const request = require('request-promise-native'); const request = require('request-promise-native');
const MicrosoftGraph = require('@microsoft/microsoft-graph-client'); const MicrosoftGraph = require('@microsoft/microsoft-graph-client');
const path = require('path');
/** /**
* @fileoverview General Bots server core. * @fileoverview General Bots server core.
*/ */
/** /**
* BASIC system class for extra manipulation of bot behaviour. * BASIC system class for extra manipulation of bot behaviour.
*/ */
export class SystemKeywords { export class SystemKeywords {
/** /**
@ -259,7 +261,7 @@ export class SystemKeywords {
let document = await this.internalGetDocument(client, baseUrl, path, file); let document = await this.internalGetDocument(client, baseUrl, path, file);
// Creates workbook session that will be discarded. // Creates workbook session that will be discarded.
let sheets = await client let sheets = await client
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets`) .api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets`)
.get(); .get();
@ -357,25 +359,29 @@ export class SystemKeywords {
let [baseUrl, client] = await this.internalGetDriveClient(); let [baseUrl, client] = await this.internalGetDriveClient();
const botId = this.min.instance.botId; const botId = this.min.instance.botId;
const path = urlJoin(`/${botId}.gbai/${botId}.gbdata`, name); let path = `/${botId}.gbai/${botId}.gbdata`;
// Extracts each part of path to call create folder to each
// one of them.
name = name.replace(/\\/gi, '/');
const parts = name.split('/');
let lastFolder = null;
await CollectionUtil.asyncForEach(parts, async item => {
path = urlJoin(path, item);
return new Promise<any>((resolve, reject) => {
const body = { const body = {
"name": name, "name": name,
"folder": {}, "folder": {},
"@microsoft.graph.conflictBehavior": "rename" "@microsoft.graph.conflictBehavior": "rename"
}; };
client lastFolder = await client
.api(`${baseUrl}/drive/root:/${path}:/children`) .api(`${baseUrl}/drive/root:/${path}:/children`)
.post(body, (err, res) => { .post(body);
if (err) {
reject(err);
}
else {
resolve(res);
}
});
}); });
return lastFolder;
} }
/** /**
@ -427,44 +433,51 @@ export class SystemKeywords {
* COPY "template.xlsx", "reports\" + customerName + "\final.xlsx" * COPY "template.xlsx", "reports\" + customerName + "\final.xlsx"
* *
*/ */
public async copyFile() { public async copyFile(src, dest) {
let [] = await this.internalGetDriveClient();
// const botId = this.min.instance.botId; let [baseUrl, client] = await this.internalGetDriveClient();
// const path = urlJoin(`/${botId}.gbai/${botId}.gbdata`, name); const botId = this.min.instance.botId;
// const body = const root = urlJoin(`/${botId}.gbai/${botId}.gbdata`);
// { const srcPath = urlJoin(root, src);
// "parentReference": { driveId: gbaiDest.parentReference.driveId, id: gbaiDest.id }, const dstPath = urlJoin(`/${botId}.gbai/${botId}.gbdata`, dest);
// "name": `${botName}.${kind}`
// } let folder;
// const packageName = `${templateName.split('.')[0]}.${kind}`; if (dest.indexOf('/') !== -1)
// try { {
// const src = await client.api( // `${baseUrl}/drive/root:/${source}`) const pathOnly = path.dirname(dest);
// .get(); folder = await this.createFolder(pathOnly);
// return await client.api( // `${baseUrl}/drive/items/${src.id}/copy`) }
// .post(body); else
// } catch (error) { {
// if (error.code === "itemNotFound") { folder = await client.api(
// } else if (error.code === "nameAlreadyExists") { `${baseUrl}/drive/root:/${root}`)
// let src = await client.api( // `${baseUrl}/drive/root:/${templateName}/${packageName}:/children`) .get();
// .get(); }
// const dstName = `${botName}.gbai/${botName}.${kind}`;
// let dst = await client.api(`${baseUrl}/drive/root:/${dstName}`) try {
// .get(); const srcFile = await client.api(
// await CollectionUtil.asyncForEach(src.value, async item => { `${baseUrl}/drive/root:/${srcPath}`)
// const body = .get();
// {
// "parentReference": { driveId: dst.parentReference.driveId, id: dst.id } const destFile =
// } {
// await client.api( // `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${templateId}/drive/items/${item.id}/copy`) "parentReference": { driveId: folder.parentReference.driveId, id: folder.id },
// .post(body); "name": `${dest}`
// }); }
// }
// else { return await client.api(
// GBLog.error(error); `${baseUrl}/drive/items/${srcFile.id}/copy`)
// throw error; .post(destFile);
// }
// } } catch (error) {
if (error.code === "itemNotFound") {
GBLog.info(`BASIC: COPY source file not found: ${srcPath}.`);
} else if (error.code === "nameAlreadyExists") {
GBLog.info(`BASIC: COPY destination file already exists: ${dstPath}.`);
}
throw error;
}
} }
/** /**