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

feat(helpers): add document archiver #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/falcor/transforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,21 @@ export function remove ( collection, user, _id ) {
});
}


export function archiveDocument(collection, _id, userId) {
return this.flatMap(db => {
db = db.mongo.collection(collection);
return db.findOneAndUpdate(
{_id: _id},
{
$set: {
archived: true,
archived_at: Date.now(),
archiver: userId,
}
},
{ returnOriginal: false }
)
})
.map(r => r.value);
}
80 changes: 80 additions & 0 deletions src/falcor/transforms/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import test from 'tape';

import database from '../../db';
import {archiveDocument} from './index'
import {generateId} from '../../utils'

const dbconf = {
mongodb: {
uri: process.env.MONGO_URI || 'mongodb://localhost:27017/dev',
},

neo4j: {
uri: process.env.NEO_URI || 'bolt://localhost',
},
};

test( 'archiveDocument', t => {
const db = database( dbconf );
const worldA = {_id: generateId(),title: "Test World A"};
const worldB = {_id: generateId(),title: "Test World B"};
const invalidWorld = {_id: generateId(),title: "Invalid world"};
const userId = "testingId";
const collection = 'world';
let neo, mongo, actual, expected;

db
.map( db => {
neo = db.neo;
mongo = db.mongo;
return mongo;
})
.flatMap( mongo => {
return mongo.collection(collection).insertMany( [worldA,worldB] )
})
.flatMap( () => {
return db::archiveDocument(collection,worldA._id,userId)
})
.flatMap( document => {

expected = worldA._id;
actual = document._id;
t.equal(actual, expected,'should have the same _id that has been passed to archiveDocument');

expected = userId;
actual = document.archiver;
t.equal(actual, expected,'should match the userId that has been passed to archiveDocument');

expected = true;
actual = document.archived;
t.equal(actual, expected,'should be archived')

return mongo.collection(collection).findOne({_id : worldB._id})
})
.flatMap(document => {

expected = worldB;
actual = document;
t.deepEqual(actual, expected,'should not change when the _id does not match the document _id');

return mongo.collection(collection).deleteMany(
{
_id : { $in: [worldA._id,worldB._id]}
}
)
})
.flatMap(r =>
db::archiveDocument(collection,invalidWorld._id,userId)
)
.subscribe((document) => {

expected = null;
actual = document;
t.equal(actual, expected,'should not exist when the document is invalid');

t.end();

mongo.close().then( () => neo.close( () => neo.disconnect() ));
})
});