Skip to content

Commit

Permalink
add create account functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Tris Setiawan committed Apr 29, 2016
1 parent 3e41825 commit e846710
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 18 deletions.
23 changes: 20 additions & 3 deletions app/middlewares/db-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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');
Expand Down
91 changes: 76 additions & 15 deletions app/services/accounts-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 };

Expand All @@ -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 => {
Expand All @@ -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
};
}

}

0 comments on commit e846710

Please sign in to comment.