From 8b1caaddda64ea860b066b440fa6a8640e78f822 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Thu, 15 Jun 2017 11:26:15 +0200 Subject: [PATCH] Fixed the overwrite behavior in the 'StandardResource.standardMoveTo' static method --- lib/resource/std/StandardResource.js | 52 +++++++++++++++++------- src/resource/std/StandardResource.ts | 59 +++++++++++++++++++--------- 2 files changed, 78 insertions(+), 33 deletions(-) diff --git a/lib/resource/std/StandardResource.js b/lib/resource/std/StandardResource.js index f571d048..8e3515cf 100644 --- a/lib/resource/std/StandardResource.js +++ b/lib/resource/std/StandardResource.js @@ -99,22 +99,44 @@ var StandardResource = (function () { callback(null); }; StandardResource.standardMoveTo = function (resource, parent, newName, overwrite, setName, callback) { - if (parent === resource.parent) { - resource.rename(newName, function (e, oldName, newName) { - callback(e); - }); - return; - } - StandardResource.standardRemoveFromParent(resource, function (e) { - if (e) { - callback(e); - } - else { - setName(newName); - parent.addChild(resource, function (e) { - callback(e); + parent.getChildren(function (e, children) { + new Workflow_1.Workflow() + .each(children, function (child, cb) { return child.webName(function (e, name) { + if (e) + cb(e); + else if (name === newName && !overwrite) + cb(Errors_1.Errors.ResourceAlreadyExists); + else if (name === newName && overwrite) + cb(null, child); + else + cb(); + }); }) + .error(callback) + .done(function (conflictingChildren) { + conflictingChildren = conflictingChildren.filter(function (c) { return !!c; }); + new Workflow_1.Workflow() + .each(conflictingChildren, function (child, cb) { return child.delete(cb); }) + .error(callback) + .done(function () { + if (parent === resource.parent) { + resource.rename(newName, function (e, oldName, newName) { + callback(e); + }); + return; + } + StandardResource.standardRemoveFromParent(resource, function (e) { + if (e) { + callback(e); + } + else { + setName(newName); + parent.addChild(resource, function (e) { + callback(e); + }); + } + }); }); - } + }); }); }; return StandardResource; diff --git a/src/resource/std/StandardResource.ts b/src/resource/std/StandardResource.ts index c315de68..fb5a37b2 100644 --- a/src/resource/std/StandardResource.ts +++ b/src/resource/std/StandardResource.ts @@ -160,26 +160,49 @@ export abstract class StandardResource implements IResource } public static standardMoveTo(resource : IResource, parent : IResource, newName : string, overwrite : boolean, setName : (name : string) => void, callback : SimpleCallback) { - if(parent === resource.parent) - { - resource.rename(newName, (e, oldName, newName) => { - callback(e); - }) - return; - } + parent.getChildren((e, children) => { + new Workflow() + .each(children, (child, cb) => child.webName((e, name) => { + if(e) + cb(e); + else if(name === newName && !overwrite) + cb(Errors.ResourceAlreadyExists); + else if(name === newName && overwrite) + cb(null, child); + else + cb(); + })) + .error(callback) + .done((conflictingChildren) => { + conflictingChildren = conflictingChildren.filter((c) => !!c); - StandardResource.standardRemoveFromParent(resource, (e) => { - if(e) - { - callback(e); - } - else - { - setName(newName); - parent.addChild(resource, (e) => { - callback(e); + new Workflow() + .each(conflictingChildren, (child : IResource, cb) => child.delete(cb)) + .error(callback) + .done(() => { + if(parent === resource.parent) + { + resource.rename(newName, (e, oldName, newName) => { + callback(e); + }) + return; + } + + StandardResource.standardRemoveFromParent(resource, (e) => { + if(e) + { + callback(e); + } + else + { + setName(newName); + parent.addChild(resource, (e) => { + callback(e); + }) + } + }) + }) }) - } }) } }