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

Add more routes #19

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions src/models/allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const AllocationSchema = new Schema({
profile: {type: String, required: true},
bucket_id: {type: String, required: true},
round_id: {type: String, required: true},
user_id: {type: Number, required: true},
date: {type: Number, required: true},
allocator: {type: String, required: true},
deleted: {type: Boolean, required: true},
Expand Down
54 changes: 38 additions & 16 deletions src/routers/allocations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class AllocationsRouter {

init () {
console.log ('Starting allocations router...');

// ---------------- GET ----------------//
this.express.route ('/').get (async (req, res, next) => {
try {
Expand All @@ -21,6 +22,43 @@ export class AllocationsRouter {
return next ({status: 500, data: e});
}
});

this.express.route ('/buckets/:bucket_id').get (async (req, res, next) => {
try {
const {bucket_id} = req.params;
if (!bucket_id) {
throw new Error ('Must supply bucket_id param');
}
const data = await this.services.allocations.getAllocationsByBucketId (
bucket_id
);
return next ({status: 200, data});
} catch (e) {
console.log ('There was a problem with your query: ', e);
return next ({status: 500, data: e});
}
});


this.express.route ('/getAllocationsByUsers').get (async (req, res, next) => {
try {
console.log('req.params', req.body)
const { users, roundId } = req.body;
if (!users || !roundId) {
throw new Error ('Must supply users and roundId params');
}
const data = await this.services.allocations.getAllocationsByUsers (
users,
roundId
);
return next ({status: 200, data});
} catch (e) {
console.log ('There was a problem with your query: ', e);
return next ({status: 500, data: e});
}
});


// ---------------- CREATE ----------------//
this.express.route ('/create').post (async (req, res, next) => {
try {
Expand Down Expand Up @@ -48,21 +86,5 @@ export class AllocationsRouter {
}
});

// ---------------- GET BY BucketID ----------------//
this.express.route ('/buckets/:bucket_id').get (async (req, res, next) => {
try {
const {bucket_id} = req.params;
if (!bucket_id) {
throw new Error ('Must supply bucket_id param');
}
const data = await this.services.allocations.getAllocationsByBucketId (
bucket_id
);
return next ({status: 200, data});
} catch (e) {
console.log ('There was a problem with your query: ', e);
return next ({status: 500, data: e});
}
});
}
}
29 changes: 27 additions & 2 deletions src/routers/buckets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import express from 'express';

export class BucketsRouter {

constructor(services) {
Expand All @@ -22,6 +21,15 @@ export class BucketsRouter {
}
});

this.express.route('/byEventAndAllocationType').get(async (req, res, next) => {
try {
const data = await this.services.buckets.getBucketByAllocationTypeAndEventId(req.query);
return next({ status: 200, data });
} catch (e) {
return next({ status: 500, data: e });
}
});

this.express.route('/:eventId/:basedEventId/:allocationType').get(async (req, res, next) => {
try {
const { eventId, basedEventId, allocationType } = req.params
Expand All @@ -36,13 +44,30 @@ export class BucketsRouter {
// ---------------- CREATE ----------------//
this.express.route('/create').post(async (req, res, next) => {
try {
// const data = await this.services.rounds.getBuckets(req.query);
const data = await this.services.buckets.createBucket(req.body);
return next({ status: 200, data });
} catch (e) {
console.log('There was a problem with your query: ', e)
return next({ status: 500, data: e });
}
});
this.express.route('/updateMany').post(async (req, res, next) => {
try {
const data = await this.services.buckets.updateBuckets(req.body);
return next({ status: 200, data });
} catch (e) {
console.log('There was a problem with your query: ', e)
return next({ status: 500, data: e });
}
});
this.express.route('/:id').get(async (req, res, next) => {
try {
const { id } = req.params
const data = await this.services.buckets.getBucketById(id);
return next({ status: 200, data });
} catch (e) {
return next({ status: 500, data: e });
}
});
}
}
17 changes: 14 additions & 3 deletions src/services/allocations.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Allocation from '../models/allocation';

import Round from '../models/round'
class AllocationService {
getAllocations() {
return Allocation.find({});
Expand All @@ -13,8 +13,8 @@ class AllocationService {
}

updateAllocation(data) {
var query = { _id: data._id };
return Allocation.findOneAndUpdate(query, data, { upsert: true });
const { _id } = data
return Allocation.findByIdAndUpdate(_id, data);
}

getAllocationsByBucketId(bucket_id) {
Expand All @@ -25,6 +25,17 @@ class AllocationService {
bucket_id,
});
}

async getAllocationsByUsers(users, round_id) {
let results = []
for (let user of users) {
results.push({
user_id: user.user_id,
allocations: await Allocation.find({ user_id: user.user_id, round_id })
})
}
return results
}
}

export default new AllocationService();
62 changes: 57 additions & 5 deletions src/services/buckets.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ import Bucket from '../models/bucket';
import Round from '../models/round';
import Allocation from '../models/allocation'
class BucketService {
// --------------- GET ----------------//
getBuckets() {
return Bucket.find({});
}
createBucket(data) {
const { event_id, based_on_event_id, allocation_type } = data;
const query = { event_id, based_on_event_id, allocation_type };
return Bucket.findOneAndUpdate(query, data, { upsert: true });

getBucketById(bucket_id) {
return Bucket.find({ _id: bucket_id })
}

getBucketByAllocationTypeAndEventId(query){
const { event_id, based_on_event_id, allocation_type, group_type } = query
if (!event_id || !based_on_event_id || !allocation_type || !group_type)
throw new Error('your query is missing a required parameter');
return Bucket.find({
event_id,
based_on_event_id,
allocation_type,
group_type
})
}

async sumByAllocation(event_id, based_on_event_id, allocation_type) {
Expand All @@ -24,14 +36,54 @@ class BucketService {
for (const bucket of buckets) {
const { _id, group_id } = bucket;
const allocations = await Allocation.find({ bucket_id: _id })
console.log('_id', _id);
res[group_id] = {
...bucket.toJSON(),
allocations
}
}
return res;
}
// --------------- CREATE ----------------//
createBucket(data) {
const { event_id, based_on_event_id, allocation_type } = data;
const query = { event_id, based_on_event_id, allocation_type };
return Bucket.findOneAndUpdate(query, data, { upsert: true });
}
// get array of requests to create of update buckets
async updateBuckets(data){
const { requests } = data
const result = {
sucess: [],
failed: []
}
for (const request of requests) {
const { group_type, allocation_type, event_id, based_on_event_id, group_id, group_name, amount } = request
request.active = request.active || true;
if(!group_type || !allocation_type || !event_id || !based_on_event_id || !group_id || !group_name || !amount) {
result.failed.push({
group_id,
status: 'failed',
e: 'missing parameter'
} )}
else {
const query = { group_type, allocation_type, event_id, based_on_event_id, group_id }
try {
const res = await Bucket.findOneAndUpdate(query, request, {upsert: true})
result.sucess.push({
group_id,
status: 'success'
});
} catch (e) {
result.failed.push({
group_id,
status: 'failed',
e: e.stack
});
}
}
}
return result;
}
}

export default new BucketService();