From e84671000b91c681606e1becf9c9f8ca947b1dec Mon Sep 17 00:00:00 2001 From: Tris Setiawan Date: Fri, 29 Apr 2016 19:34:25 +0700 Subject: [PATCH] add create account functionality --- app/middlewares/db-wrapper.js | 23 ++++++-- app/services/accounts-service.js | 91 ++++++++++++++++++++++++++------ 2 files changed, 96 insertions(+), 18 deletions(-) diff --git a/app/middlewares/db-wrapper.js b/app/middlewares/db-wrapper.js index 1309962..f6c6599 100644 --- a/app/middlewares/db-wrapper.js +++ b/app/middlewares/db-wrapper.js @@ -2,12 +2,29 @@ module.exports = function (db) { var dbwrapper = function (request, response, next) { var r = request; r.db = db; - + // get single data; r.single = function (collectionName, query) { return r.db.collection(collectionName).find(query).limit(1).next(); }; - + // insert single data + r.insert = function (collectionName, newData) { + + return new Promise(function (resolve, reject) { + var collection = r.db.collection(collectionName); + collection + .insertOne(newData) + .then(result => { + var id = result.insertedId; + r.single(collectionName, { _id: id }) + .then(data => { + resolve(data); + }) + .catch(e => reject(e)); + }) + .catch(e => reject(e)); + }); + } // update single data r.update = function (collectionName, query, updateObject) { @@ -18,7 +35,7 @@ module.exports = function (db) { if (doc._stamp != updateObject._stamp) reject('stamp mismatch'); else { - var Base = require('./app/models/base'); + var Base = require('capital-models').Base; if (updateObject instanceof Base) { console.log('base'); diff --git a/app/services/accounts-service.js b/app/services/accounts-service.js index 3670fa5..aa13fa1 100644 --- a/app/services/accounts-service.js +++ b/app/services/accounts-service.js @@ -2,13 +2,16 @@ var Service = require('./service'); var Account = require('capital-models').identity.Account; +var UserProfile = require('capital-models').identity.UserProfile; +var UserOrganizationInfo = require('capital-models').identity.UserOrganizationInfo; +var map = require('capital-models').map; module.exports = class AccountService extends Service { constructor() { super("1.0.0"); this.collectionName = "accounts"; - } - + } + all(request, response, next) { var collection = request.db.collection(this.collectionName); collection.find().toArray() @@ -19,7 +22,7 @@ module.exports = class AccountService extends Service { .catch(e => next(e)); } - get(request, response, next) { + get(request, response, next) { var username = request.params.username; var query = { username: username }; @@ -30,24 +33,40 @@ module.exports = class AccountService extends Service { }) .catch(e => next(e)); } - + create(request, response, next) { - var body = request.body; - var data = Object.assign(new User(), body); - var collection = request.db.collection(this.collectionName); - data.dob = data.dob ? new Date(data.dob) : new Date(); - data.stamp('actor', 'agent'); + var body = this.getData(request.body); + var account = Object.assign(new Account(), body.account); + var profile = Object.assign(new UserProfile(), body.profile); + var info = Object.assign(new UserOrganizationInfo(), body.info); - collection.insertOne(data) - .then(result => { - response.locals.data = result; - next(); + var accountCollection = request.db.collection(map.identity.account); + profile.dob = profile.dob ? new Date(profile.dob) : new Date(); + account.stamp('actor', 'agent'); + profile.stamp('actor', 'agent'); + info.stamp('actor', 'agent'); + + + request.insert(map.identity.account, account) + .then(accountResult => { + profile.accountId = accountResult._id; + request.insert(map.identity.userProfile, profile) + .then(profileResult => { + info.accountId = accountResult._id; + request.insert(map.identity.userOrganizationInfo, info) + .then(infoResult => { + response.locals.data = { account: accountResult, profile: profileResult, info: infoResult }; + next(); + }) + .catch(e => next(e)); + }) + .catch(e => next(e)); }) - .catch(e => next(e)) + .catch(e => next(e)); } update(request, response, next) { - var data = request.body; + var data = request.body; var query = { 'username': data.username }; request.update(this.collectionName, query, data) .then(doc => { @@ -61,4 +80,46 @@ module.exports = class AccountService extends Service { console.log('delete:called'); response.send(''); } + + + + + /// + + getData(body) { + var data = { + account: this.getAccount(body), + profile: this.getUserProfile(body), + info: this.getUserOrganizationInfo(body) + }; + + return data; + } + getAccount(body) { + return { + _id: body._id, + username: body.username, + password: body.password, + locked: body.locked, + confirmed: body.confirmed, + roles: [] + } + } + getUserProfile(body) { + return { + accountId: body.accountId, + name: body.name, + dob: body.dob, + gender: body.gender + }; + } + getUserOrganizationInfo(body) { + return { + accountId: body.accountId, + nik: body.nik, + initial: body.initial, + department: body.department + }; + } + } \ No newline at end of file