Skip to content

Commit 08da667

Browse files
authored
Merge pull request #73 from mustachebash/update/ticket-management
update/ticket management
2 parents 9c68074 + 8cc3476 commit 08da667

File tree

5 files changed

+83
-45
lines changed

5 files changed

+83
-45
lines changed

lib/routes/orders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ ordersRouter
119119
});
120120

121121
ordersRouter
122-
.post('/:id/transfers', authorizeUser, requiresPermission('admin'), async ctx => {
122+
.post('/:id/transfers', authorizeUser, requiresPermission('write'), async ctx => {
123123
if(!ctx.request.body) ctx.throw(400);
124124

125125
try {

lib/routes/promos.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ promosRouter
1717
ctx.throw(e);
1818
}
1919
})
20-
.post('/', authorizeUser, requiresPermission('admin'), async ctx => {
20+
.post('/', authorizeUser, requiresPermission('write'), async ctx => {
2121
try {
2222
const promo = await createPromo({...ctx.request.body, createdBy: ctx.user.id});
2323

@@ -59,7 +59,7 @@ promosRouter
5959
ctx.throw(e);
6060
}
6161
})
62-
.delete('/:id', authorizeUser, requiresPermission('admin'), async ctx => {
62+
.delete('/:id', authorizeUser, requiresPermission('write'), async ctx => {
6363
try {
6464
const promo = await updatePromo(ctx.params.id, {updatedBy: ctx.user.id, status: 'disabled'});
6565

lib/services/auth.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ module.exports = {
215215
'root',
216216
'god',
217217
'admin',
218+
'write',
218219
'doorman',
219220
'read'
220221
];

lib/services/orders.js

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -441,51 +441,84 @@ module.exports = {
441441
return order;
442442
},
443443

444-
// Full refunds only
445-
refundOrder() {
446-
throw new OrdersServiceError('Not yet implemented', 'NOT_IMPLEMENTED');
447-
// const order = await run(r.table('orders').get(id));
444+
// Full order refund
445+
async refundOrder(id) {
446+
let order;
447+
try {
448+
[order] = await sql`
449+
SELECT
450+
o.status AS order_status,
451+
t.id AS transaction_id,
452+
t.type AS transaction_type,
453+
t.processor_transaction_id,
454+
processor,
455+
t.parent_transaction_id
456+
FROM orders AS o
457+
LEFT JOIN transactions AS t
458+
ON o.id = t.order_id
459+
WHERE
460+
o.id = ${id};
461+
`;
462+
} catch(e) {
463+
throw new OrdersServiceError('Could not query orders', 'UNKNOWN', e);
464+
}
448465

449-
// if(!order) throw new OrdersServiceError('Order not found', 'NOT_FOUND');
466+
if (!order) throw new OrdersServiceError('Order not found', 'NOT_FOUND');
467+
if (order.orderStatus !== 'complete') throw new OrdersServiceError(`Cannot refund order with status: ${order.orderStatus}`, 'REFUND_NOT_ALLOWED');
450468

451-
// let refundResponse, orderStatus;
452-
// try {
453-
// const {status: processorStatus} = await gateway.order.find(order.braintreeTransactionId);
454-
455-
// if(['settled', 'settling'].includes(processorStatus)) {
456-
// orderStatus = 'refunded';
457-
// refundResponse = await gateway.order.refund(order.braintreeTransactionId);
458-
// } else {
459-
// orderStatus = 'voided';
460-
// refundResponse = await gateway.order.void(order.braintreeTransactionId);
461-
// }
462-
// } catch(e) {
463-
// throw new OrdersServiceError('Order not refunded', 'BRAINTREE_ERROR', e);
464-
// }
469+
const newTransaction = {
470+
id: uuidV4(),
471+
orderId: id,
472+
processor: order.processor,
473+
parentTransactionId: order.transactionId
474+
};
465475

466-
// // Sometimes the call is successful but the refund is not
467-
// if(!refundResponse.success) throw new OrdersServiceError('Order not refunded', 'BRAINTREE_ERROR', refundResponse);
476+
if(order.processor === 'braintree') {
477+
let processorResponse;
478+
try {
479+
const {status: processorStatus} = await gateway.transaction.find(order.processorTransactionId);
480+
481+
if(['settled', 'settling'].includes(processorStatus)) {
482+
processorResponse = await gateway.transaction.refund(order.processorTransactionId);
483+
newTransaction.type = 'refund';
484+
} else {
485+
processorResponse = await gateway.transaction.void(order.processorTransactionId);
486+
newTransaction.type = 'void';
487+
}
488+
} catch(e) {
489+
throw new OrdersServiceError('Order not refunded', 'BRAINTREE_ERROR', e);
490+
}
468491

469-
// // Mark the order as refunded in our system, disable the guests and tickets
470-
// try {
471-
// // Synchronize this
472-
// const updated = r.now();
473-
// await Promise.all([
474-
// run(r.table('orders').get(id).update({status: orderStatus, updatedBy: username, updated})),
475-
// run(r.table('guests').filter({orderId: order.id}).update({status: 'archived', updatedBy: username, updated})),
476-
// run(r.table('tickets')
477-
// .getAll(
478-
// r.args(r.table('guests').filter({orderId: order.id})('id').coerceTo('array')),
479-
// {index: 'guestId'}
480-
// )
481-
// .update({status: 'disabled', updatedBy: username, updated}))
482-
// ]);
483-
// } catch(e) {
484-
// console.error(e);
485-
// throw new OrdersServiceError('Order voiding failed', 'UNKNOWN');
486-
// }
492+
// Sometimes the call is successful but the refund is not
493+
if(!processorResponse.success) throw new OrdersServiceError('Order not refunded', 'BRAINTREE_ERROR', processorResponse);
494+
495+
newTransaction.processorTransactionId = processorResponse.transaction.id;
496+
newTransaction.processorCreatedAt = processorResponse.transaction.createdAt;
497+
newTransaction.amount = Number(processorResponse.transaction.amount);
498+
} else {
499+
throw new OrdersServiceError('Order not refunded', 'INVALID_PROCESSOR');
500+
}
487501

488-
// return refundResponse;
502+
// Mark the order as canceled in our system, archive the guests
503+
try {
504+
await Promise.all([
505+
sql`
506+
INSERT INTO transactions ${sql(newTransaction)}
507+
`,
508+
sql`
509+
UPDATE orders
510+
SET status = 'canceled'
511+
WHERE id = ${id}
512+
`,
513+
sql`
514+
UPDATE guests
515+
SET status = 'archived', updated = now()
516+
WHERE order_id = ${id}
517+
`
518+
]);
519+
} catch(e) {
520+
throw new OrdersServiceError('Order voiding failed', 'UNKNOWN', e);
521+
}
489522
},
490523

491524
transferOrderTickets() {

lib/services/tickets.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ module.exports = {
3535
SELECT
3636
g.id,
3737
g.admission_tier,
38+
e.id as event_id,
3839
e.name as event_name,
3940
e.date as event_date,
40-
g.ticket_seed
41+
g.ticket_seed,
42+
g.status
4143
FROM guests as g
4244
LEFT JOIN events as e
4345
ON e.id = g.event_id
@@ -57,8 +59,10 @@ module.exports = {
5759
tickets.push({
5860
id: guest.id,
5961
admissionTier: guest.admissionTier,
62+
eventId: guest.eventId,
6063
eventName: guest.eventName,
61-
eventDate: guest.eventDate
64+
eventDate: guest.eventDate,
65+
status: guest.status
6266
// qrCode
6367
});
6468
}

0 commit comments

Comments
 (0)