@mongoosejs/studio 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +2 -0
- package/backend/actions/Model/exportQueryResults.js +66 -0
- package/backend/actions/Model/getDocument.js +30 -0
- package/backend/actions/Model/getDocuments.js +47 -0
- package/backend/actions/Model/index.js +7 -0
- package/backend/actions/Model/listModels.js +5 -0
- package/backend/actions/Model/updateDocument.js +32 -0
- package/backend/actions/index.js +3 -0
- package/backend/index.js +12 -0
- package/backend/netlify.js +28 -0
- package/express.js +36 -0
- package/frontend/index.js +36 -0
- package/frontend/public/app.js +4012 -0
- package/frontend/public/images/mongoose.svg +35 -0
- package/frontend/public/index.html +21 -0
- package/frontend/public/style.css +34 -0
- package/frontend/src/api.js +52 -0
- package/frontend/src/appendCSS.js +11 -0
- package/frontend/src/async-button/async-button.html +3 -0
- package/frontend/src/async-button/async-button.js +39 -0
- package/frontend/src/detail-array/detail-array.html +3 -0
- package/frontend/src/detail-array/detail-array.js +16 -0
- package/frontend/src/detail-default/detail-default.html +3 -0
- package/frontend/src/detail-default/detail-default.js +19 -0
- package/frontend/src/document/document.css +39 -0
- package/frontend/src/document/document.html +42 -0
- package/frontend/src/document/document.js +63 -0
- package/frontend/src/edit-default/edit-default.html +3 -0
- package/frontend/src/edit-default/edit-default.js +20 -0
- package/frontend/src/export-query-results/export-query-results.css +0 -0
- package/frontend/src/export-query-results/export-query-results.html +11 -0
- package/frontend/src/export-query-results/export-query-results.js +34 -0
- package/frontend/src/index.js +43 -0
- package/frontend/src/list-array/list-array.css +8 -0
- package/frontend/src/list-array/list-array.html +3 -0
- package/frontend/src/list-array/list-array.js +23 -0
- package/frontend/src/list-default/list-default.html +3 -0
- package/frontend/src/list-default/list-default.js +19 -0
- package/frontend/src/list-subdocument/list-subdocument.html +3 -0
- package/frontend/src/list-subdocument/list-subdocument.js +9 -0
- package/frontend/src/modal/modal.css +75 -0
- package/frontend/src/modal/modal.html +12 -0
- package/frontend/src/modal/modal.js +10 -0
- package/frontend/src/models/models.css +108 -0
- package/frontend/src/models/models.html +53 -0
- package/frontend/src/models/models.js +107 -0
- package/frontend/src/navbar/navbar.css +162 -0
- package/frontend/src/navbar/navbar.html +10 -0
- package/frontend/src/navbar/navbar.js +11 -0
- package/frontend/src/routes.js +19 -0
- package/frontend/webpack.config.js +36 -0
- package/package.json +21 -0
- package/test.js +264 -0
package/test.js
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const express = require('express');
|
|
4
|
+
const mongoose = require('mongoose');
|
|
5
|
+
|
|
6
|
+
mongoose.connect('mongodb://localhost:27017/terravera_test');
|
|
7
|
+
|
|
8
|
+
const cartItemSchema = new mongoose.Schema({
|
|
9
|
+
_id: false,
|
|
10
|
+
productId: { type: String, required: true },
|
|
11
|
+
product: new mongoose.Schema({
|
|
12
|
+
name: String,
|
|
13
|
+
slug: String,
|
|
14
|
+
price: Number,
|
|
15
|
+
displayPrice: String,
|
|
16
|
+
productDescription: String,
|
|
17
|
+
color: String,
|
|
18
|
+
image: String,
|
|
19
|
+
suitableFor: [String],
|
|
20
|
+
productLabel: String,
|
|
21
|
+
sds: String,
|
|
22
|
+
productLine: String,
|
|
23
|
+
setupFee: Number,
|
|
24
|
+
}),
|
|
25
|
+
price: { type: Number, required: true },
|
|
26
|
+
quantity: { type: Number, required: true },
|
|
27
|
+
recurring: { type: String, enum: ['monthly'] },
|
|
28
|
+
lease: { type: Boolean }
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
cartItemSchema.virtual('lineItemName').get(function lineItemName() {
|
|
32
|
+
if (this.lease) {
|
|
33
|
+
return `${this.product.name} (Lease)`;
|
|
34
|
+
} else if (this.recurring) {
|
|
35
|
+
const recurring = this.recurring.slice(0, 1).toUpperCase() + this.recurring.slice(1);
|
|
36
|
+
return `${this.product.name} (${recurring})`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return this.product.name;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const lineItemSchema = new mongoose.Schema({
|
|
43
|
+
description: { type: String, required: true },
|
|
44
|
+
price: { type: Number, required: true },
|
|
45
|
+
quantity: { type: Number, required: true, min: 0 },
|
|
46
|
+
total: {
|
|
47
|
+
type: Number,
|
|
48
|
+
required: true
|
|
49
|
+
}, // price * quantity rounded to 2 decimal places
|
|
50
|
+
waived: {
|
|
51
|
+
type: Boolean,
|
|
52
|
+
required: true,
|
|
53
|
+
default: false
|
|
54
|
+
},
|
|
55
|
+
productId: {
|
|
56
|
+
type: String
|
|
57
|
+
},
|
|
58
|
+
tags: [{
|
|
59
|
+
type: String,
|
|
60
|
+
enum: ['setup_fee', 'promo_code', 'sales_tax', 'lease']
|
|
61
|
+
}]
|
|
62
|
+
}, { id: false, toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
63
|
+
|
|
64
|
+
lineItemSchema.virtual('prettyTotal').get(function prettyTotal() {
|
|
65
|
+
const total = this.total || 0;
|
|
66
|
+
return this.total;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const cartSchema = new mongoose.Schema({
|
|
70
|
+
items: [cartItemSchema],
|
|
71
|
+
lineItems: [lineItemSchema],
|
|
72
|
+
orderId: { type: mongoose.ObjectId, ref: 'Order' },
|
|
73
|
+
stripePaymentIntentId: String,
|
|
74
|
+
stripePaymentMethodId: String
|
|
75
|
+
}, { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
76
|
+
|
|
77
|
+
cartSchema.methods.applyPromoCode = function applyPromoCode(promoCode) {
|
|
78
|
+
this.promoCodes.push(promoCode);
|
|
79
|
+
if (promoCode.waivesTag != null) {
|
|
80
|
+
for (const li of this.lineItems) {
|
|
81
|
+
if (!li.tags.includes(promoCode.waivesTag)) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
li.waived = true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (promoCode.flatDiscount != null) {
|
|
88
|
+
this.lineItems.push({
|
|
89
|
+
description: promoCode.code,
|
|
90
|
+
price: -promoCode.flatDiscount,
|
|
91
|
+
quantity: 1,
|
|
92
|
+
total: -promoCode.flatDiscount,
|
|
93
|
+
tags: ['promo_code']
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (promoCode.percentDiscount != null) {
|
|
97
|
+
if (promoCode.discountOnProductIds != null) {
|
|
98
|
+
const lineItems = this.lineItems.filter((li) => {
|
|
99
|
+
if (promoCode.leaseOnly) {
|
|
100
|
+
return promoCode.discountOnProductIds.indexOf(li.productId) > -1 && li.tags.includes('lease');
|
|
101
|
+
}
|
|
102
|
+
if (promoCode.purchaseOnly) {
|
|
103
|
+
return promoCode.discountOnProductIds.indexOf(li.productId) > -1 && !li.tags.includes('lease');
|
|
104
|
+
}
|
|
105
|
+
return promoCode.discountOnProductIds.indexOf(li.productId) > -1;
|
|
106
|
+
});
|
|
107
|
+
if (lineItems.length) {
|
|
108
|
+
for(let i = 0; i < lineItems.length; i++) {
|
|
109
|
+
this.lineItems.push({
|
|
110
|
+
description: promoCode.code,
|
|
111
|
+
price: -(lineItems[i].total * promoCode.percentDiscount / 100).toFixed(2),
|
|
112
|
+
quantity: 1,
|
|
113
|
+
total: -(lineItems[i].total * promoCode.percentDiscount / 100).toFixed(2),
|
|
114
|
+
tags: ['promo_code']
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
this.lineItems.push({
|
|
120
|
+
description: promoCode.code,
|
|
121
|
+
price: -(this.totalWithoutTax * promoCode.percentDiscount / 100).toFixed(2),
|
|
122
|
+
quantity: 1,
|
|
123
|
+
total: -(this.totalWithoutTax * promoCode.percentDiscount / 100).toFixed(2),
|
|
124
|
+
tags: ['promo_code']
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (promoCode.waivesProductsExceptIds != null && promoCode.waivesProductsExceptIds.length > 0) {
|
|
129
|
+
let numWaived = 0;
|
|
130
|
+
|
|
131
|
+
const promoCodeLineItem = {
|
|
132
|
+
description: promoCode.code,
|
|
133
|
+
price: 0,
|
|
134
|
+
quantity: 1,
|
|
135
|
+
total: 0,
|
|
136
|
+
tags: ['promo_code']
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
for (const li of this.lineItems) {
|
|
140
|
+
if (li.productId && !promoCode.waivesProductsExceptIds.includes(li.productId)) {
|
|
141
|
+
const quantityToWaive = promoCode.maxWaivedProducts == null ?
|
|
142
|
+
li.quantity :
|
|
143
|
+
Math.min(promoCode.maxWaivedProducts - numWaived, li.quantity);
|
|
144
|
+
const discount = +(li.price * quantityToWaive).toFixed(2);
|
|
145
|
+
promoCodeLineItem.price = +(promoCodeLineItem.price - discount).toFixed(2);
|
|
146
|
+
promoCodeLineItem.total = promoCodeLineItem.price;
|
|
147
|
+
numWaived += quantityToWaive;
|
|
148
|
+
}
|
|
149
|
+
if (promoCode.maxWaivedProducts != null && numWaived >= promoCode.maxWaivedProducts) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (promoCodeLineItem.total !== 0) {
|
|
155
|
+
this.lineItems.push(promoCodeLineItem);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
cartSchema.pre('save', function sortLineItems() {
|
|
161
|
+
this.lineItems.sort((li1, li2) => {
|
|
162
|
+
if (li1.productId != null && li2.productId == null) {
|
|
163
|
+
return -1;
|
|
164
|
+
}
|
|
165
|
+
if (li1.productId == null && li2.productId != null) {
|
|
166
|
+
return 1;
|
|
167
|
+
}
|
|
168
|
+
return 0;
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
cartSchema.virtual('subtotal').get(function subtotal() {
|
|
173
|
+
return this.lineItems.
|
|
174
|
+
filter(li => li.productId != null).
|
|
175
|
+
filter(li => !li.waived).
|
|
176
|
+
reduce((sum, li) => {
|
|
177
|
+
return +(sum + li.total).toFixed(2);
|
|
178
|
+
}, 0);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
cartSchema.virtual('promoDiscount').get(function promoDiscount() {
|
|
182
|
+
return this.lineItems.
|
|
183
|
+
filter(li => li.tags.includes('promo_code')).
|
|
184
|
+
reduce((sum, li) => {
|
|
185
|
+
return +(sum + li.total).toFixed(2);
|
|
186
|
+
}, 0);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
cartSchema.virtual('setupFee').get(function setupFee() {
|
|
190
|
+
return this.lineItems.
|
|
191
|
+
filter(li => li.tags.includes('setup_fee')).
|
|
192
|
+
filter(li => !li.waived).
|
|
193
|
+
reduce((total, li) => {
|
|
194
|
+
return +(total + (li.total)).toFixed(2);
|
|
195
|
+
}, 0);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
cartSchema.virtual('salesTax').
|
|
199
|
+
get(function salesTax() {
|
|
200
|
+
return this.lineItems.
|
|
201
|
+
filter(li => li.tags.includes('sales_tax')).
|
|
202
|
+
filter(li => !li.waived).
|
|
203
|
+
reduce((total, li) => {
|
|
204
|
+
return +(total + (li.total)).toFixed(2);
|
|
205
|
+
}, 0);
|
|
206
|
+
}).
|
|
207
|
+
set(function updateSalesTax(v) {
|
|
208
|
+
const index = this.lineItems.findIndex(li => li.tags.includes('sales_tax'));
|
|
209
|
+
if (index === -1) {
|
|
210
|
+
this.lineItems.push({
|
|
211
|
+
description: 'Sales Tax',
|
|
212
|
+
price: v,
|
|
213
|
+
total: v,
|
|
214
|
+
quantity: 1,
|
|
215
|
+
tags: ['sales_tax']
|
|
216
|
+
});
|
|
217
|
+
} else {
|
|
218
|
+
this.lineItems[index].price = v;
|
|
219
|
+
this.lineItems[index].total = v;
|
|
220
|
+
this.lineItems[index].quantity = 1;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
cartSchema.virtual('totalWithoutTax').get(function totalWithoutTax() {
|
|
225
|
+
const promoDiscount = this.promoDiscount || 0;
|
|
226
|
+
const setupFee = this.setupFee || 0;
|
|
227
|
+
return +(this.subtotal + promoDiscount + setupFee).toFixed(2);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
cartSchema.virtual('total').get(function total() {
|
|
231
|
+
const salesTax = this.salesTax || 0;
|
|
232
|
+
return +(this.totalWithoutTax + salesTax).toFixed(2);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
cartSchema.virtual('salesTaxLineItem').get(function salesTaxLineItem() {
|
|
236
|
+
return this.lineItems.find(li => li.tags.includes('sales_tax'));
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
cartSchema.virtual('subtotalLineItem').get(function subtotalLineItem() {
|
|
240
|
+
return {
|
|
241
|
+
description: 'Subtotal',
|
|
242
|
+
price: this.subtotal,
|
|
243
|
+
quantity: 1,
|
|
244
|
+
total: this.subtotal
|
|
245
|
+
};
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
cartSchema.virtual('promoCodeLineItems').get(function promoCodeLineItems() {
|
|
249
|
+
return this.lineItems.filter(li => li.tags.includes('promo_code'));
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
cartSchema.virtual('setupFeeLineItems').get(function setupFeeLineItems() {
|
|
253
|
+
return this.lineItems.filter(li => li.tags.includes('setup_fee'));
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const Cart = mongoose.model('Cart', cartSchema);
|
|
257
|
+
|
|
258
|
+
module.exports = Cart;
|
|
259
|
+
|
|
260
|
+
const app = express();
|
|
261
|
+
app.use('/admin', require('./express')('/admin/api', null, { watch: true }));
|
|
262
|
+
|
|
263
|
+
app.listen(5005);
|
|
264
|
+
console.log('Listening on 5005');
|