mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 12:30:06 +00:00
Merge pull request #807 from sasjs/issue-245
fix(deploy-service-pack): fixed redeployment
This commit is contained in:
@@ -29,11 +29,33 @@ describe('SASViyaApiClient', () => {
|
|||||||
jest
|
jest
|
||||||
.spyOn(requestClient, 'get')
|
.spyOn(requestClient, 'get')
|
||||||
.mockImplementation(() => Promise.reject('Not Found'))
|
.mockImplementation(() => Promise.reject('Not Found'))
|
||||||
|
|
||||||
const error = await sasViyaApiClient
|
const error = await sasViyaApiClient
|
||||||
.createFolder('test', '/foo')
|
.createFolder('test', '/foo')
|
||||||
.catch((e: any) => e)
|
.catch((e: any) => e)
|
||||||
|
|
||||||
expect(error).toBeInstanceOf(RootFolderNotFoundError)
|
expect(error).toBeInstanceOf(RootFolderNotFoundError)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should throw an error when ', async () => {
|
||||||
|
const testMessage1 = 'test message 1'
|
||||||
|
const testMessage2 = 'test message 2.'
|
||||||
|
|
||||||
|
jest.spyOn(requestClient, 'post').mockImplementation(() => {
|
||||||
|
return Promise.reject({
|
||||||
|
message: testMessage1,
|
||||||
|
response: { data: { message: testMessage2 }, status: 409 }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const error = await sasViyaApiClient
|
||||||
|
.createFolder('test', '/foo')
|
||||||
|
.catch((e: any) => e)
|
||||||
|
|
||||||
|
const expectedError = `${testMessage1}. ${testMessage2} To override, please set "isForced" to "true".`
|
||||||
|
|
||||||
|
expect(error).toEqual(expectedError)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const setupMocks = () => {
|
const setupMocks = () => {
|
||||||
|
|||||||
@@ -378,12 +378,14 @@ export class SASViyaApiClient {
|
|||||||
isForced?: boolean
|
isForced?: boolean
|
||||||
): Promise<Folder> {
|
): Promise<Folder> {
|
||||||
const logger = process.logger || console
|
const logger = process.logger || console
|
||||||
|
|
||||||
if (!parentFolderPath && !parentFolderUri) {
|
if (!parentFolderPath && !parentFolderUri) {
|
||||||
throw new Error('Path or URI of the parent folder is required.')
|
throw new Error('Path or URI of the parent folder is required.')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parentFolderUri && parentFolderPath) {
|
if (!parentFolderUri && parentFolderPath) {
|
||||||
parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken)
|
parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken)
|
||||||
|
|
||||||
if (!parentFolderUri) {
|
if (!parentFolderUri) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`Parent folder at path '${parentFolderPath}' is not present.`
|
`Parent folder at path '${parentFolderPath}' is not present.`
|
||||||
@@ -394,6 +396,7 @@ export class SASViyaApiClient {
|
|||||||
parentFolderPath.lastIndexOf('/')
|
parentFolderPath.lastIndexOf('/')
|
||||||
)
|
)
|
||||||
const newFolderName = `${parentFolderPath.split('/').pop()}`
|
const newFolderName = `${parentFolderPath.split('/').pop()}`
|
||||||
|
|
||||||
if (newParentFolderPath === '') {
|
if (newParentFolderPath === '') {
|
||||||
throw new RootFolderNotFoundError(
|
throw new RootFolderNotFoundError(
|
||||||
parentFolderPath,
|
parentFolderPath,
|
||||||
@@ -401,20 +404,24 @@ export class SASViyaApiClient {
|
|||||||
accessToken
|
accessToken
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'`
|
`Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'`
|
||||||
)
|
)
|
||||||
|
|
||||||
const parentFolder = await this.createFolder(
|
const parentFolder = await this.createFolder(
|
||||||
newFolderName,
|
newFolderName,
|
||||||
newParentFolderPath,
|
newParentFolderPath,
|
||||||
undefined,
|
undefined,
|
||||||
accessToken
|
accessToken
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`Parent folder '${newFolderName}' has been successfully created.`
|
`Parent folder '${newFolderName}' has been successfully created.`
|
||||||
)
|
)
|
||||||
|
|
||||||
parentFolderUri = `/folders/folders/${parentFolder.id}`
|
parentFolderUri = `/folders/folders/${parentFolder.id}`
|
||||||
} else if (isForced && accessToken) {
|
} else if (isForced) {
|
||||||
const folderPath = parentFolderPath + '/' + folderName
|
const folderPath = parentFolderPath + '/' + folderName
|
||||||
const folderUri = await this.getFolderUri(folderPath, accessToken)
|
const folderUri = await this.getFolderUri(folderPath, accessToken)
|
||||||
|
|
||||||
@@ -427,8 +434,8 @@ export class SASViyaApiClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { result: createFolderResponse } =
|
const { result: createFolderResponse } = await this.requestClient
|
||||||
await this.requestClient.post<Folder>(
|
.post<Folder>(
|
||||||
`/folders/folders?parentFolderUri=${parentFolderUri}`,
|
`/folders/folders?parentFolderUri=${parentFolderUri}`,
|
||||||
{
|
{
|
||||||
name: folderName,
|
name: folderName,
|
||||||
@@ -436,12 +443,34 @@ export class SASViyaApiClient {
|
|||||||
},
|
},
|
||||||
accessToken
|
accessToken
|
||||||
)
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
const { message, response } = err
|
||||||
|
|
||||||
|
if (message && response && response.data && response.data.message) {
|
||||||
|
const { status } = response
|
||||||
|
const { message: responseMessage } = response.data
|
||||||
|
const messages = [message, responseMessage].map((mes: string) =>
|
||||||
|
/\.$/.test(mes) ? mes : `${mes}.`
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!isForced && status === 409) {
|
||||||
|
messages.push(`To override, please set "isForced" to "true".`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const errMessage = messages.join(' ')
|
||||||
|
|
||||||
|
throw errMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err
|
||||||
|
})
|
||||||
|
|
||||||
// update folder map with newly created folder.
|
// update folder map with newly created folder.
|
||||||
await this.populateFolderMap(
|
await this.populateFolderMap(
|
||||||
`${parentFolderPath}/${folderName}`,
|
`${parentFolderPath}/${folderName}`,
|
||||||
accessToken
|
accessToken
|
||||||
)
|
)
|
||||||
|
|
||||||
return createFolderResponse
|
return createFolderResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -900,7 +929,7 @@ export class SASViyaApiClient {
|
|||||||
return `/folders/folders/${folderDetails.id}`
|
return `/folders/folders/${folderDetails.id}`
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getRecycleBinUri(accessToken: string) {
|
private async getRecycleBinUri(accessToken?: string) {
|
||||||
const url = '/folders/folders/@myRecycleBin'
|
const url = '/folders/folders/@myRecycleBin'
|
||||||
|
|
||||||
const { result: folder } = await this.requestClient
|
const { result: folder } = await this.requestClient
|
||||||
@@ -984,7 +1013,7 @@ export class SASViyaApiClient {
|
|||||||
sourceFolder: string,
|
sourceFolder: string,
|
||||||
targetParentFolder: string,
|
targetParentFolder: string,
|
||||||
targetFolderName: string,
|
targetFolderName: string,
|
||||||
accessToken: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
// If target path is an existing folder, than keep source folder name, othervise rename it with given target folder name
|
// If target path is an existing folder, than keep source folder name, othervise rename it with given target folder name
|
||||||
const sourceFolderName = sourceFolder.split('/').pop() as string
|
const sourceFolderName = sourceFolder.split('/').pop() as string
|
||||||
@@ -1051,7 +1080,7 @@ export class SASViyaApiClient {
|
|||||||
* @param folderPath - the full path (eg `/Public/example/deleteThis`) of the folder to be deleted.
|
* @param folderPath - the full path (eg `/Public/example/deleteThis`) of the folder to be deleted.
|
||||||
* @param accessToken - an access token for authorizing the request.
|
* @param accessToken - an access token for authorizing the request.
|
||||||
*/
|
*/
|
||||||
public async deleteFolder(folderPath: string, accessToken: string) {
|
public async deleteFolder(folderPath: string, accessToken?: string) {
|
||||||
const recycleBinUri = await this.getRecycleBinUri(accessToken)
|
const recycleBinUri = await this.getRecycleBinUri(accessToken)
|
||||||
const folderName = folderPath.split('/').pop() || ''
|
const folderName = folderPath.split('/').pop() || ''
|
||||||
const date = new Date()
|
const date = new Date()
|
||||||
|
|||||||
19
src/SASjs.ts
19
src/SASjs.ts
@@ -337,13 +337,16 @@ export default class SASjs {
|
|||||||
sasApiClient?: SASViyaApiClient,
|
sasApiClient?: SASViyaApiClient,
|
||||||
isForced?: boolean
|
isForced?: boolean
|
||||||
) {
|
) {
|
||||||
if (sasApiClient)
|
if (sasApiClient) {
|
||||||
return await sasApiClient.createFolder(
|
return await sasApiClient.createFolder(
|
||||||
folderName,
|
folderName,
|
||||||
parentFolderPath,
|
parentFolderPath,
|
||||||
parentFolderUri,
|
parentFolderUri,
|
||||||
accessToken
|
accessToken,
|
||||||
|
isForced
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return await this.sasViyaApiClient!.createFolder(
|
return await this.sasViyaApiClient!.createFolder(
|
||||||
folderName,
|
folderName,
|
||||||
parentFolderPath,
|
parentFolderPath,
|
||||||
@@ -783,13 +786,11 @@ export default class SASjs {
|
|||||||
this.isMethodSupported('deployServicePack', [ServerType.SasViya])
|
this.isMethodSupported('deployServicePack', [ServerType.SasViya])
|
||||||
|
|
||||||
let sasApiClient: any = null
|
let sasApiClient: any = null
|
||||||
|
|
||||||
if (serverUrl || appLoc) {
|
if (serverUrl || appLoc) {
|
||||||
if (!serverUrl) {
|
if (!serverUrl) serverUrl = this.sasjsConfig.serverUrl
|
||||||
serverUrl = this.sasjsConfig.serverUrl
|
if (!appLoc) appLoc = this.sasjsConfig.appLoc
|
||||||
}
|
|
||||||
if (!appLoc) {
|
|
||||||
appLoc = this.sasjsConfig.appLoc
|
|
||||||
}
|
|
||||||
if (this.sasjsConfig.serverType === ServerType.SasViya) {
|
if (this.sasjsConfig.serverType === ServerType.SasViya) {
|
||||||
sasApiClient = new SASViyaApiClient(
|
sasApiClient = new SASViyaApiClient(
|
||||||
serverUrl,
|
serverUrl,
|
||||||
@@ -807,11 +808,13 @@ export default class SASjs {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let sasClientConfig: any = null
|
let sasClientConfig: any = null
|
||||||
|
|
||||||
if (this.sasjsConfig.serverType === ServerType.SasViya) {
|
if (this.sasjsConfig.serverType === ServerType.SasViya) {
|
||||||
sasClientConfig = this.sasViyaApiClient!.getConfig()
|
sasClientConfig = this.sasViyaApiClient!.getConfig()
|
||||||
} else if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
} else if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
||||||
sasClientConfig = this.sas9ApiClient!.getConfig()
|
sasClientConfig = this.sas9ApiClient!.getConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
serverUrl = sasClientConfig.serverUrl
|
serverUrl = sasClientConfig.serverUrl
|
||||||
appLoc = sasClientConfig.rootFolderName as string
|
appLoc = sasClientConfig.rootFolderName as string
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user