1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-03 02:30:06 +00:00

refactor: refactored moveFolder and deleteFolder functions

This commit is contained in:
Yury Shkoda
2020-09-08 10:36:16 +03:00
parent df86b2e700
commit 12835893b1
10 changed files with 3096 additions and 34 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -76,7 +76,7 @@
<section class="tsd-index-section "> <section class="tsd-index-section ">
<h3>Modules</h3> <h3>Modules</h3>
<ul class="tsd-index-list"> <ul class="tsd-index-list">
<li class="tsd-kind-module tsd-is-not-exported"><a href="modules/reflection-641.html" class="tsd-kind-icon"><em>Module</em></a></li> <li class="tsd-kind-module tsd-is-not-exported"><a href="modules/reflection-640.html" class="tsd-kind-icon"><em>Module</em></a></li>
<li class="tsd-kind-module"><a href="modules/types.html" class="tsd-kind-icon">types</a></li> <li class="tsd-kind-module"><a href="modules/types.html" class="tsd-kind-icon">types</a></li>
<li class="tsd-kind-module"><a href="modules/utils.html" class="tsd-kind-icon">utils</a></li> <li class="tsd-kind-module"><a href="modules/utils.html" class="tsd-kind-icon">utils</a></li>
</ul> </ul>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -402,10 +402,7 @@ export class SASViyaApiClient {
} else if (isForced && accessToken && !this.isForceDeploy) { } else if (isForced && accessToken && !this.isForceDeploy) {
this.isForceDeploy = true this.isForceDeploy = true
const oldFolderName = parentFolderPath?.split('/').pop() || '' await this.deleteFolder(parentFolderPath, accessToken)
const parentFolderId = parentFolderUri?.split('/').pop() || ''
await this.deleteFolder(parentFolderId, oldFolderName, accessToken)
const newParentFolderPath = parentFolderPath.substring( const newParentFolderPath = parentFolderPath.substring(
0, 0,
@@ -1121,7 +1118,7 @@ export class SASViyaApiClient {
return `/folders/folders/${folder.id}` return `/folders/folders/${folder.id}`
} }
private async getRecycleBin(accessToken: string) { private async getRecycleBinUri(accessToken: string) {
const url = '/folders/folders/@myRecycleBin' const url = '/folders/folders/@myRecycleBin'
const requestInfo = { const requestInfo = {
method: 'GET', method: 'GET',
@@ -1140,23 +1137,34 @@ export class SASViyaApiClient {
if (!folder) return undefined if (!folder) return undefined
return folder return `/folders/folders/${folder.id}`
} }
/** /**
* Changes location of the folder. * Moves a Viya folder to a new location. The folder may be renamed at the same time.
* @param from - current location of the folder * @param sourceFolder - The full path to the source folder to be moved (eg `/Public/example/myFolder`)
* @param to - new location of the folder * @param targetParentFolder - The _parent_ folder to which the `sourceFolder` will be moved (eg `/Public/newDestination`). To move a folder, a user has to have write permissions in targetParentFolder. If moving to recycle bin, 'targetParentFolder' will be a uri.
* @param folderName - folder name * @param targetFolderName - The name of the "moved" folder. If left blank, the original folder name will be used (eg `myFolder` in `/Public/newDestination/myFolder` for the example above). Optional field.
* @param accessToken - an access token for an authorized request * @param accessToken - an access token for authorizing the request
*/ */
public async moveFolder( public async moveFolder(
from: string, sourceFolder: string,
to: string, targetParentFolder: string,
folderName: string, targetFolderName: string,
accessToken: string accessToken: string
) { ) {
const url = '/folders/folders/' + from const sourceFolderUri = await this.getFolderUri(sourceFolder, accessToken)
// checks if 'targetParentFolder' is already a uri
const targetParentFolderUri = /^\/folders\/folders\//.test(
targetParentFolder
)
? targetParentFolder
: await this.getFolderUri(targetParentFolder, accessToken)
const sourceFolderId = sourceFolderUri?.split('/').pop()
const url = sourceFolderUri
const requestInfo = { const requestInfo = {
method: 'PATCH', method: 'PATCH',
headers: { headers: {
@@ -1164,9 +1172,9 @@ export class SASViyaApiClient {
Authorization: 'Bearer ' + accessToken Authorization: 'Bearer ' + accessToken
}, },
body: JSON.stringify({ body: JSON.stringify({
id: from, id: sourceFolderId,
name: folderName, name: targetFolderName,
parentFolderUri: '/folders/folders/' + to parentFolderUri: targetParentFolderUri
}) })
} }
@@ -1183,23 +1191,21 @@ export class SASViyaApiClient {
} }
/** /**
* Moves folder to the recycle bin * For performance (and in case of accidental error) the `deleteFolder` function does not actually delete the folder (and all it's content and subfolder content). Instead the folder is simply moved to the recycle bin. Deletion time will be added to the folder name.
* @param folderId - an id of the folder * @param folderPath - the full path (eg `/Public/example/deleteThis`) of the folder to be deleted.
* @param folderName - folder name * @param accessToken - an access token for authorizing the request.
* @param accessToken - an access token for an authorized request
*/ */
public async deleteFolder( public async deleteFolder(folderPath: string, accessToken: string) {
folderId: string, const recycleBinUri = await this.getRecycleBinUri(accessToken)
folderName: string, const folderName = folderPath.split('/').pop() || ''
accessToken: string const date = new Date()
) { const timeMark = date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
const recycleBin = await this.getRecycleBin(accessToken) const deletedFolderName = folderName + ' ' + timeMark
const recycleBinId = recycleBin?.id as string
const movedFolder = await this.moveFolder( const movedFolder = await this.moveFolder(
folderId, folderPath,
recycleBinId, recycleBinUri!,
folderName, deletedFolderName,
accessToken accessToken
) )