From 71c03ca67bae3b0dd28041849ffa16f611bf1fcf Mon Sep 17 00:00:00 2001 From: Mark Fridman Date: Fri, 15 Feb 2019 15:03:54 +0200 Subject: [PATCH 1/5] another round bites the dust --- src/routers/buckets.js | 20 +++++++++++++++++++- src/services/buckets.service.js | 27 +++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/routers/buckets.js b/src/routers/buckets.js index e5cd256..94e48f5 100644 --- a/src/routers/buckets.js +++ b/src/routers/buckets.js @@ -1,5 +1,4 @@ import express from 'express'; - export class BucketsRouter { constructor(services) { @@ -22,6 +21,16 @@ export class BucketsRouter { } }); + this.express.route('/byEventAndAllocationType').get(async (req, res, next) => { + try { + console.log('req.query', req.query) + 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 @@ -44,5 +53,14 @@ export class BucketsRouter { 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 }); + } + }); } } \ No newline at end of file diff --git a/src/services/buckets.service.js b/src/services/buckets.service.js index df40272..11394d4 100644 --- a/src/services/buckets.service.js +++ b/src/services/buckets.service.js @@ -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) { @@ -32,6 +44,13 @@ class BucketService { } 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 }); + } + } export default new BucketService(); From 52e803c85d7f27f324fc62b56e53b890caefb00f Mon Sep 17 00:00:00 2001 From: Mark Fridman Date: Mon, 25 Feb 2019 23:29:46 +0200 Subject: [PATCH 2/5] added array of buckets route --- src/routers/buckets.js | 10 +++++++- src/services/allocations.service.js | 4 +-- src/services/buckets.service.js | 40 +++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/routers/buckets.js b/src/routers/buckets.js index 94e48f5..acfc662 100644 --- a/src/routers/buckets.js +++ b/src/routers/buckets.js @@ -45,7 +45,6 @@ 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) { @@ -53,6 +52,15 @@ export class BucketsRouter { 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 diff --git a/src/services/allocations.service.js b/src/services/allocations.service.js index dee7502..a77d40c 100644 --- a/src/services/allocations.service.js +++ b/src/services/allocations.service.js @@ -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) { diff --git a/src/services/buckets.service.js b/src/services/buckets.service.js index 11394d4..9661126 100644 --- a/src/services/buckets.service.js +++ b/src/services/buckets.service.js @@ -36,7 +36,6 @@ 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 @@ -50,7 +49,44 @@ class BucketService { 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: [] + } + console.log(requests.length) + for (const request of requests) { + console.log(request) + 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' + }); + console.log('res', res) + } catch (e) { + result.failed.push({ + group_id, + status: 'failed', + e: e.stack + }); + } + } + } + return result; + } } export default new BucketService(); From 1236fa2ca5a197ad40a6c677c32cacf484ce6ea3 Mon Sep 17 00:00:00 2001 From: Mark Fridman Date: Tue, 26 Feb 2019 10:25:01 +0200 Subject: [PATCH 3/5] remove logs --- src/services/buckets.service.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/buckets.service.js b/src/services/buckets.service.js index 9661126..1697a65 100644 --- a/src/services/buckets.service.js +++ b/src/services/buckets.service.js @@ -58,7 +58,6 @@ class BucketService { } console.log(requests.length) for (const request of requests) { - console.log(request) 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) { From 401405dbc3cfe9a55644e9a66c335f93b3edec93 Mon Sep 17 00:00:00 2001 From: Mark Fridman Date: Tue, 26 Feb 2019 10:26:04 +0200 Subject: [PATCH 4/5] remove more logs --- src/routers/buckets.js | 1 - src/services/buckets.service.js | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/routers/buckets.js b/src/routers/buckets.js index acfc662..b6cccb4 100644 --- a/src/routers/buckets.js +++ b/src/routers/buckets.js @@ -23,7 +23,6 @@ export class BucketsRouter { this.express.route('/byEventAndAllocationType').get(async (req, res, next) => { try { - console.log('req.query', req.query) const data = await this.services.buckets.getBucketByAllocationTypeAndEventId(req.query); return next({ status: 200, data }); } catch (e) { diff --git a/src/services/buckets.service.js b/src/services/buckets.service.js index 1697a65..9b3fbe6 100644 --- a/src/services/buckets.service.js +++ b/src/services/buckets.service.js @@ -56,7 +56,6 @@ class BucketService { sucess: [], failed: [] } - console.log(requests.length) 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; @@ -74,7 +73,6 @@ class BucketService { group_id, status: 'success' }); - console.log('res', res) } catch (e) { result.failed.push({ group_id, From 9e644f6dbf7878e74d15df4a895133b48a2a5144 Mon Sep 17 00:00:00 2001 From: Mark Fridman Date: Mon, 4 Mar 2019 22:31:37 +0200 Subject: [PATCH 5/5] add route fix schema --- src/models/allocation.js | 1 + src/routers/allocations.js | 54 ++++++++++++++++++++--------- src/services/allocations.service.js | 13 ++++++- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/models/allocation.js b/src/models/allocation.js index fadb777..c0c7a6d 100644 --- a/src/models/allocation.js +++ b/src/models/allocation.js @@ -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}, diff --git a/src/routers/allocations.js b/src/routers/allocations.js index c170b91..a723a5d 100644 --- a/src/routers/allocations.js +++ b/src/routers/allocations.js @@ -10,6 +10,7 @@ export class AllocationsRouter { init () { console.log ('Starting allocations router...'); + // ---------------- GET ----------------// this.express.route ('/').get (async (req, res, next) => { try { @@ -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 { @@ -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}); - } - }); } } diff --git a/src/services/allocations.service.js b/src/services/allocations.service.js index a77d40c..62e8893 100644 --- a/src/services/allocations.service.js +++ b/src/services/allocations.service.js @@ -1,5 +1,5 @@ import Allocation from '../models/allocation'; - +import Round from '../models/round' class AllocationService { getAllocations() { return Allocation.find({}); @@ -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();