Skip to content

Commit

Permalink
Fixed the overwrite behavior in the 'StandardResource.standardMoveTo'…
Browse files Browse the repository at this point in the history
… static method
  • Loading branch information
AdrienCastex committed Jun 15, 2017
1 parent 4be3904 commit 8b1caad
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 33 deletions.
52 changes: 37 additions & 15 deletions lib/resource/std/StandardResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 41 additions & 18 deletions src/resource/std/StandardResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
}
})
})
})
}
})
}
}

0 comments on commit 8b1caad

Please sign in to comment.