Skip to content

Commit fa5b00a

Browse files
committed
feat: csv on order complete
1 parent ee6d48a commit fa5b00a

File tree

8 files changed

+141
-25
lines changed

8 files changed

+141
-25
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public/doc_images
88
config.json
99
docs
1010
log/*
11-
**/.DS_Store
11+
**/.DS_Store
12+
mnt/27_Synbio
13+
rethinkdb_data

config-example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"gif"
1616
],
1717
"tmpDir": "/tmp",
18+
"csvDir": "/mnt/synbio/brady_printing/",
1819
"baseURL": "http://127.0.0.1:3000",
1920
"ldap": {
2021
"url": "ldap://my-dc-controller:389",

controllers/shoppingCart.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Order = require('../models/order');
66
// const Log = require('../lib/log');
77
// const async = require('async');
88
const Email = require('../lib/email');
9+
const Csv = require('../lib/csv');
910
const Flash = require('../lib/flash');
1011
const config = require('../config.json');
1112

@@ -171,7 +172,7 @@ ShoppingCart.placeOrder = (req, res) => {
171172
const isCostApplicable =
172173
config.isPricingAvailable && (config.admins.includes(username) || req.user.company !== 'TSL')
173174

174-
console.log(`Cost is ${isCostApplicable ? '' : 'not'} applied`)
175+
//console.log(`Cost is ${isCostApplicable ? '' : 'not'} applied`)
175176
// if (!isCostApplicable) {
176177
// if (!config.isPricingAvailable){
177178
// console.log('\t...because pricing has been turned off in local settings')
@@ -206,16 +207,28 @@ ShoppingCart.placeOrder = (req, res) => {
206207

207208
Promise.all(saving).then(()=> {
208209
savedOrder.getTypes().then((orderWithTypes)=> {
209-
Email.newOrder(orderWithTypes, req.user).then(()=> {
210-
cart.empty().then(()=> {
211-
Flash.success(req, 'Order successfully placed');
212-
return res.redirect('/cart');
210+
211+
Csv.newOrder(orderWithTypes, req.user).then(() => {
212+
213+
Email.newOrder(orderWithTypes, req.user).then(()=> {
214+
215+
cart.empty().then(()=> {
216+
217+
Flash.success(req, 'Order successfully placed');
218+
return res.redirect('/cart');
219+
220+
}).catch((err)=> {
221+
return renderError(err, res);
222+
})
223+
213224
}).catch((err)=> {
214225
return renderError(err, res);
215-
})
226+
});
227+
216228
}).catch((err)=> {
217229
return renderError(err, res);
218230
});
231+
219232
}).catch((err)=> {
220233
return renderError(err, res);
221234
});
@@ -228,7 +241,6 @@ ShoppingCart.placeOrder = (req, res) => {
228241
return renderError(err, res);
229242
});
230243

231-
232244
}).catch((err)=> {
233245
return renderError(err, res);
234246
});

lib/csv.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const config = require('../config.json');
2+
const path = require('path');
3+
const Order = require('../models/order');
4+
const fs = require('fs');
5+
const { stringify } = require('csv-stringify');
6+
7+
const csv = {};
8+
9+
/**
10+
* Send csv about new order to config path mounted volume
11+
* @param order
12+
* @param user
13+
*/
14+
csv.newOrder = (order, user) => new Promise((good, bad)=> {
15+
16+
const csvFilePath = config.csvDir + order.janCode + '.csv';
17+
const writableStream = fs.createWriteStream(csvFilePath);
18+
19+
const columns = [
20+
'item',
21+
'concentration',
22+
'synbio_id',
23+
'url'
24+
];
25+
26+
/** ORDER
27+
complete: false,
28+
costCode: 'tempo-rar-y',
29+
createdAt: 2023-02-10T17:22:37.770Z,
30+
id: '0b34b576-6789-4112-bcaa-24cfac520460',
31+
items: [
32+
model {
33+
cartID: '1677fa3e-3389-414d-b179-d3614abcead7',
34+
id: '6e358e02-eba0-4976-aa57-3c72d78d142c',
35+
largeScale: false,
36+
orderID: '0b34b576-6789-4112-bcaa-24cfac520460',
37+
quantity: 1,
38+
typeID: '1d3f4762-f7c9-4a25-a615-925918b2b86b',
39+
type: [model]
40+
}
41+
],
42+
janCode: '2870',
43+
pricePerUnit: '5',
44+
totalCost: '5',
45+
totalQuantity: '1',
46+
username: 'deeks'
47+
*/
48+
49+
const rows = order.items.map(item => {
50+
return [
51+
item.type.name,
52+
item.type.concentration,
53+
item.type.synBioID,
54+
config.baseURL + 'premade/item/' + item.typeID, // bit HACK-y
55+
]
56+
});
57+
58+
const formattedRows = [
59+
[ 'Order', order.janCode, 'User', user.username ],
60+
...rows,
61+
[ 'Order', order.janCode, 'Print Job', 'Finished']
62+
];
63+
64+
const stringifier = stringify({header: true, columns: columns});
65+
66+
formattedRows.forEach(formattedRow => {
67+
stringifier.write(formattedRow);
68+
});
69+
70+
stringifier.pipe(writableStream);
71+
console.log('Finished writing data')
72+
return good();
73+
});
74+
75+
module.exports = csv;

lib/email.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ email.newOrder = (order, user) => new Promise((good, bad)=> {
2828
addresses.push(username + config.email.emailDomain);
2929
});
3030

31-
// delete this next time, it does nothing
32-
//user.isAdmin = admins.includes(user.username);
33-
34-
console.log('Order jancode sent to NEW ORDER email:', order.janCode)
31+
console.log('New order jancode on email sent:', order.janCode)
3532

3633
Order
3734
.count()
@@ -40,7 +37,6 @@ email.newOrder = (order, user) => new Promise((good, bad)=> {
4037
console.log('count is:', count)
4138
})
4239
.then(_ => {
43-
4440

4541
const newOrder = transporter.templateSender(new EmailTemplate(path.join(templatesDir, 'new-order')), {
4642
from: config.email.from

models/order.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ Order.define('getTypes', function () {
5757
})
5858

5959
});
60-
6160
});
6261

63-
6462
Order.pre('save', function (next) {
6563
const order = this;
6664
if (!order.janCode) {
@@ -70,13 +68,9 @@ Order.pre('save', function (next) {
7068
.then(count => {
7169
const newJanCode = count + 1;
7270
const newJanCodeStr = newJanCode.toString();
73-
if (typeof(newJanCodeStr) !== 'string'){
74-
console.error('Did not convert JanCode to string')
75-
}
76-
if (!(newJanCode > count)){
77-
console.error('JanCode is not increased from count')
78-
}
79-
console.log('originalCount', count,'new jancode:', newJanCode, 'as string:', newJanCodeStr)
71+
72+
console.log('PRESAVE - count:', count, '+ new jancode as string:', newJanCodeStr)
73+
8074
order.janCode = newJanCodeStr;
8175
if (order.janCode != newJanCodeStr){
8276
console.error('Problems assigning order.janCode')
@@ -86,12 +80,9 @@ Order.pre('save', function (next) {
8680
.catch(err => {
8781
next(err);
8882
})
89-
9083
} else {
91-
//console.log('JanCode established as:', order.janCode, 'count not available unless added to code')
9284
next();
9385
}
94-
9586
});
9687

9788

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
"dependencies": {
2727
"body-parser": "^1.15.0",
2828
"cookie-parser": "^1.4.1",
29+
"csv": "^6.2.7",
30+
"csv-stringify": "^6.2.4",
2931
"ejs": "^2.5.2",
3032
"email-templates": "^2.5.4",
3133
"express": "^4.14.0",
3234
"express-flash": "0.0.2",
3335
"express-session": "^1.14.2",
36+
"fs": "0.0.1-security",
3437
"gravatar": "^1.6.0",
3538
"inputmask": "^5.0.3",
3639
"is-image": "1.0.1",

0 commit comments

Comments
 (0)