2016-01-16 20:06:47 +00:00
|
|
|
/* eslint-disable no-use-before-define */
|
|
|
|
|
|
|
|
|
|
import { requester } from './requester';
|
2016-01-17 03:50:54 +00:00
|
|
|
import {
|
|
|
|
|
getDocument as getDocumentFromMongo,
|
|
|
|
|
updateDocument as updateDocumentInMongo,
|
2016-01-29 11:07:46 +00:00
|
|
|
} from '../mongo';
|
2016-01-16 20:06:47 +00:00
|
|
|
import {
|
|
|
|
|
assign,
|
|
|
|
|
each,
|
|
|
|
|
isEmpty,
|
|
|
|
|
set,
|
|
|
|
|
} from 'lodash';
|
|
|
|
|
|
|
|
|
|
class ApiObject {
|
|
|
|
|
constructor (options) {
|
|
|
|
|
assign(this, options);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-16 20:33:53 +00:00
|
|
|
async update (options) {
|
|
|
|
|
if (isEmpty(options)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await updateDocumentInMongo(this._docType, this, options);
|
|
|
|
|
|
2016-01-17 01:13:33 +00:00
|
|
|
_updateLocalParameters(this, options);
|
2016-01-16 20:33:53 +00:00
|
|
|
|
|
|
|
|
return this;
|
2016-01-16 20:06:47 +00:00
|
|
|
}
|
2016-01-17 03:50:54 +00:00
|
|
|
|
|
|
|
|
async sync () {
|
|
|
|
|
let updatedDoc = await getDocumentFromMongo(this._docType, this);
|
|
|
|
|
|
|
|
|
|
assign(this, updatedDoc);
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-01-16 20:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ApiUser extends ApiObject {
|
|
|
|
|
constructor (options) {
|
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
|
|
this._docType = 'users';
|
|
|
|
|
|
|
|
|
|
let _requester = requester(this);
|
|
|
|
|
|
|
|
|
|
this.get = _requester.get;
|
|
|
|
|
this.post = _requester.post;
|
|
|
|
|
this.put = _requester.put;
|
|
|
|
|
this.del = _requester.del;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ApiGroup extends ApiObject {
|
|
|
|
|
constructor (options) {
|
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
|
|
this._docType = 'groups';
|
|
|
|
|
}
|
2016-01-21 16:38:43 +00:00
|
|
|
|
|
|
|
|
async addChat (chat) {
|
|
|
|
|
let group = this;
|
|
|
|
|
|
|
|
|
|
if (!chat) {
|
|
|
|
|
chat = {
|
|
|
|
|
id: 'Test_ID',
|
|
|
|
|
text: 'Test message',
|
|
|
|
|
flagCount: 0,
|
|
|
|
|
timestamp: Date(),
|
|
|
|
|
likes: {},
|
|
|
|
|
flags: {},
|
|
|
|
|
uuid: group.leader,
|
|
|
|
|
contributor: {},
|
|
|
|
|
backer: {},
|
|
|
|
|
user: group.leader,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let update = { chat };
|
|
|
|
|
|
2016-01-23 19:41:51 +00:00
|
|
|
return await this.update(update);
|
2016-01-21 16:38:43 +00:00
|
|
|
}
|
2016-01-16 20:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-17 16:21:35 +00:00
|
|
|
export class ApiChallenge extends ApiObject {
|
|
|
|
|
constructor (options) {
|
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
|
|
this._docType = 'challenges';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-16 20:33:53 +00:00
|
|
|
function _updateLocalParameters (doc, update) {
|
2016-01-16 20:06:47 +00:00
|
|
|
each(update, (value, param) => {
|
|
|
|
|
set(doc, param, value);
|
|
|
|
|
});
|
|
|
|
|
}
|