Skip to content

Commit 9864280

Browse files
Replace Cancel with Remove in print queue, remove canceled state, and implement Chrome-based printing
1 parent d84d55e commit 9864280

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

src/app/admin/printer-monitor/page.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ function PrinterMonitorContent() {
172172
}
173173
};
174174

175-
const handleCancel = async (orderId: string) => {
176-
if (!confirm(`Cancel order ${orderId}?`)) return;
175+
const handleRemove = async (orderId: string) => {
176+
if (!confirm(`Remove order ${orderId} from print queue?`)) return;
177177

178178
try {
179179
const response = await fetch('/api/admin/print-actions', {
@@ -182,18 +182,18 @@ function PrinterMonitorContent() {
182182
'Content-Type': 'application/json',
183183
'x-admin-email': adminEmail,
184184
},
185-
body: JSON.stringify({ orderId, reason: 'Admin cancelled order' }),
185+
body: JSON.stringify({ orderId, reason: 'Admin removed from print queue' }),
186186
});
187187

188188
const result = await response.json();
189189
if (result.success) {
190-
showSuccess('Order cancelled');
190+
showSuccess('Order removed from print queue');
191191
fetchData();
192192
} else {
193-
showError(result.error || 'Failed to cancel order');
193+
showError(result.error || 'Failed to remove order from queue');
194194
}
195195
} catch (error) {
196-
showError('Error cancelling order');
196+
showError('Error removing order from queue');
197197
}
198198
};
199199

@@ -456,10 +456,11 @@ function PrinterMonitorContent() {
456456
Reprint
457457
</button>
458458
<button
459-
onClick={() => handleCancel(order.orderId)}
459+
onClick={() => handleRemove(order.orderId)}
460460
className="text-xs bg-red-600 text-white px-2 py-1 rounded hover:bg-red-700"
461+
title="Remove from print queue"
461462
>
462-
Cancel
463+
Remove
463464
</button>
464465
</div>
465466
</div>
@@ -580,7 +581,16 @@ function PrinterMonitorContent() {
580581
</div>
581582
<p className="text-sm text-gray-700 mb-1">{order.fileName}</p>
582583
<p className="text-xs text-gray-500 mb-1">Printer: {order.printerName || 'N/A'}</p>
583-
<p className="text-xs text-gray-500">Completed: {formatDate(order.printCompletedAt)}</p>
584+
<p className="text-xs text-gray-500 mb-2">Completed: {formatDate(order.printCompletedAt)}</p>
585+
<div className="flex gap-2 mt-2">
586+
<button
587+
onClick={() => handleRemove(order.orderId)}
588+
className="text-xs bg-gray-600 text-white px-2 py-1 rounded hover:bg-gray-700"
589+
title="Remove from print queue (if printed manually)"
590+
>
591+
Remove
592+
</button>
593+
</div>
584594
</div>
585595
))}
586596
</div>

src/app/api/admin/print-actions/route.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ export async function POST(request: NextRequest) {
162162
}
163163

164164
/**
165-
* PUT /api/admin/print-actions/cancel
166-
* Cancel a pending order
165+
* PUT /api/admin/print-actions/remove
166+
* Remove order from print queue (doesn't cancel the order)
167+
* Can be used for pending, printing, or printed orders
167168
*/
168169
export async function PUT(request: NextRequest) {
169170
try {
@@ -200,33 +201,50 @@ export async function PUT(request: NextRequest) {
200201
);
201202
}
202203

203-
if (order.printStatus !== 'pending') {
204+
// Check if payment is completed - cannot remove if payment completed
205+
if (order.paymentStatus === 'completed') {
204206
return NextResponse.json(
205-
{ success: false, error: 'Only pending orders can be cancelled' },
207+
{ success: false, error: 'Cannot remove order from queue if payment is completed. Order must remain in system.' },
206208
{ status: 400 }
207209
);
208210
}
209211

210212
const previousStatus = order.printStatus;
213+
const previousOrderStatus = order.orderStatus;
214+
const previousMainStatus = order.status;
215+
216+
// Remove from print queue - doesn't cancel the order
217+
// If order was previously cancelled, reset to pending
218+
const newOrderStatus = previousOrderStatus === 'cancelled' ? 'pending' : previousOrderStatus;
219+
const newMainStatus = previousMainStatus === 'cancelled' ? 'pending_payment' : previousMainStatus;
211220

212-
// Cancel order
213221
await Order.findByIdAndUpdate(order._id, {
222+
$unset: {
223+
printStatus: '',
224+
printStartedAt: '',
225+
printCompletedAt: '',
226+
printerId: '',
227+
printerName: '',
228+
printingBy: '',
229+
printJobId: '',
230+
printingHeartbeatAt: '',
231+
printError: '',
232+
},
214233
$set: {
215-
printStatus: undefined, // Remove print status
216-
orderStatus: 'cancelled',
217-
status: 'cancelled',
234+
orderStatus: newOrderStatus,
235+
status: newMainStatus,
218236
},
219237
});
220238

221239
// Log action
222-
await logAction('cancel', orderId, auth.adminEmail!, previousStatus, 'cancelled', reason, order.printJobId);
240+
await logAction('remove_from_queue', orderId, auth.adminEmail!, previousStatus || 'none', 'removed', reason, order.printJobId);
223241

224242
return NextResponse.json({
225243
success: true,
226-
message: 'Order cancelled',
244+
message: 'Order removed from print queue',
227245
});
228246
} catch (error) {
229-
console.error('Error in cancel action:', error);
247+
console.error('Error in remove action:', error);
230248
return NextResponse.json(
231249
{
232250
success: false,

0 commit comments

Comments
 (0)