Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete draft amendments #83

Merged
merged 7 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/resolvers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,19 @@ module.exports = ({ models }) => ({ action, data, id, meta }, transaction) => {
.then(() => Project.queryWithDeleted(transaction).findById(id));
}

if (action === 'delete-amendments') {
return ProjectVersion.query(transaction).where({ projectId: id }).orderBy('createdAt', 'desc')
.then(versions => {
const granted = versions.find(v => v.status === 'granted');
wheelsandcogs marked this conversation as resolved.
Show resolved Hide resolved
return versions.filter(v => v.status !== 'granted' && v.createdAt > granted.createdAt);
})
.then(recentDraftVersions => {
return ProjectVersion.query(transaction)
wheelsandcogs marked this conversation as resolved.
Show resolved Hide resolved
.delete()
.whereIn('id', recentDraftVersions.map(v => v.id));
})
.then(() => Project.query(transaction).findById(id));
}

return resolver({ Model: models.Project, action, data, id }, transaction);
};
3 changes: 2 additions & 1 deletion test/resolvers/pil.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ describe('PIL resolver', () => {
.then(pil => {
assert.deepEqual(pil, undefined);
})
.then(() => this.models.PIL.query())
.then(() => this.models.PIL.queryWithDeleted().findById(opts.id))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just a fix for something else that's wildly out of scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was returning an empty array before, which then passed the assertion even though there was no PIL.

.then(pil => {
assert(pil.deleted);
assert(moment(pil.deleted).isValid());
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/resolvers/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ describe('Place resolver', () => {
.then(place => {
assert.deepEqual(place, undefined);
})
.then(() => this.models.Place.query())
.then(() => this.models.Place.queryWithDeleted().findById(opts.id))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna guess yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same deal here.

.then(place => {
assert(place.deleted);
assert(moment(place.deleted).isValid());
});
});
Expand Down
127 changes: 127 additions & 0 deletions test/resolvers/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const assert = require('assert');
const moment = require('moment');
const { project } = require('../../lib/resolvers');
const db = require('../helpers/db');

const profileId = 'f0835b01-00a0-4c7f-954c-13ed2ef7efd9';
const projectId = '1da9b8b7-b12b-49f3-98be-745d286949a7';

describe('Project resolver', () => {

before(() => {
this.models = db.init();
this.project = project({ models: this.models });
});

beforeEach(() => {
return db.clean(this.models)
.then(() => this.models.Establishment.query().insert({
id: 8201,
name: 'Univerty of Croydon'
}))
.then(() => this.models.Profile.query().insert({
id: profileId,
userId: 'abc123',
title: 'Dr',
firstName: 'Linford',
lastName: 'Christie',
address: '1 Some Road',
postcode: 'A1 1AA',
email: 'test1@example.com',
telephone: '01234567890'
}))
.then(() => this.models.Project.query().insert([
{
id: projectId,
status: 'active',
title: 'Hypoxy and angiogenesis in cancer therapy',
issueDate: new Date('2019-07-11').toISOString(),
expiryDate: new Date('2022-07-11').toISOString(),
licenceNumber: 'PP-627808',
establishmentId: 8201,
licenceHolderId: profileId
}
]))
.then(() => this.models.ProjectVersion.query().insert([
{
id: '8fb05730-f2ec-4d8f-8085-bbdc86937c54',
projectId,
data: {},
status: 'draft',
createdAt: new Date('2019-07-04').toISOString()
},
{
id: '68d79bb1-3573-4402-ac08-7ac27dcbb39e',
projectId,
data: {},
status: 'submitted',
createdAt: new Date('2019-07-03').toISOString()
},
{
id: 'ee871d64-cc87-470a-82d9-4a326c9c08dc',
projectId,
data: {},
status: 'draft',
createdAt: new Date('2019-07-02').toISOString()
},
{
id: '574266e5-ef34-4e34-bf75-7b6201357e75',
projectId,
data: {},
status: 'granted',
createdAt: new Date('2019-07-01').toISOString()
},
{
id: 'b497b05a-f1e0-4596-8b02-60e129e2ab49',
projectId,
data: {},
status: 'submitted',
createdAt: new Date('2019-06-04').toISOString()
},
{
id: '71e25eca-e0aa-4555-b09b-62f55b83e890',
projectId,
data: {},
status: 'granted',
createdAt: new Date('2019-06-03').toISOString()
}
]));
});

afterEach(() => db.clean(this.models));

after(() => this.models.destroy());

describe('delete amendments', () => {
it('only soft deletes versions since the most recent granted version', () => {
const opts = {
action: 'delete-amendments',
id: projectId
};
return Promise.resolve()
.then(() => this.project(opts))
.then(() => this.models.ProjectVersion.queryWithDeleted())
.then(versions => {
versions.map(version => {
if ([
'8fb05730-f2ec-4d8f-8085-bbdc86937c54',
'68d79bb1-3573-4402-ac08-7ac27dcbb39e',
'ee871d64-cc87-470a-82d9-4a326c9c08dc'
].includes(version.id)) {
assert(version.deleted);
assert(moment(version.deleted).isValid(), 'version was soft deleted');
}

if ([
'574266e5-ef34-4e34-bf75-7b6201357e75',
'b497b05a-f1e0-4596-8b02-60e129e2ab49',
'71e25eca-e0aa-4555-b09b-62f55b83e890'
].includes(version.id)) {
assert(!version.deleted, 'version was not deleted');
}
});
});
});
});

});