-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinterController.js
More file actions
170 lines (145 loc) · 4.1 KB
/
Copy pathprinterController.js
File metadata and controls
170 lines (145 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const { Printer } = require('../models');
async function syncPrinters(req, res) {
try {
const { machine_id, printers } = req.body;
if (!machine_id || !Array.isArray(printers)) {
return res.status(400).json({
success: false,
message: 'Invalid request body'
});
}
const result = await Printer.syncPrinters(machine_id, printers);
res.json({
success: true,
message: 'Printers synced successfully',
menu: "Printer Management",
data: result
});
} catch (error) {
console.error('Error syncing printers:', error);
res.status(500).json({
success: false,
message: 'Failed to sync printers'
});
}
}
async function getAllPrinters(req, res) {
try {
const printers = await Printer.findAll();
res.json({
success: true,
data: printers
});
} catch (error) {
console.error('Error fetching printers:', error);
res.status(500).json({
success: false,
message: 'Failed to fetch printers'
});
}
}
async function updatePrinterStatus(req, res) {
try {
const { machine_id, printer_name, status } = req.body;
if (!machine_id || !printer_name || !status) {
return res.status(400).json({
success: false,
message: 'Invalid request body'
});
}
if (!['online', 'offline'].includes(status)) {
return res.status(400).json({
success: false,
message: 'Invalid status value'
});
}
await Printer.updateStatus(printer_name, machine_id, status);
res.json({
success: true,
message: 'Printer status updated'
});
} catch (error) {
console.error('Error updating printer status:', error);
res.status(500).json({
success: false,
message: 'Failed to update printer status'
});
}
}
async function assignPrinterToDepartment(req, res) {
try {
const { printer_id } = req.params;
const { department } = req.body;
if (department && !['dm', 'confectionery', 'design'].includes(department)) {
return res.status(400).json({
success: false,
message: 'Invalid department'
});
}
const printer = await Printer.findById(parseInt(printer_id, 10));
if (!printer) {
return res.status(404).json({
success: false,
message: 'Printer not found'
});
}
await Printer.assignDepartment(parseInt(printer_id, 10), department || null);
const updatedPrinter = await Printer.findById(parseInt(printer_id, 10));
res.json({
success: true,
message: department ? `Printer assigned to ${department} department` : 'Printer unassigned',
data: {
printer_id: updatedPrinter.printer_id,
printer_name: updatedPrinter.printer_name,
assigned_department: updatedPrinter.assigned_department
}
});
} catch (error) {
console.error('Error assigning printer:', error);
res.status(500).json({
success: false,
message: 'Failed to assign printer'
});
}
}
async function togglePrinterActive(req, res) {
try {
const { printer_id } = req.params;
const { is_active } = req.body;
if (typeof is_active !== 'boolean') {
return res.status(400).json({
success: false,
message: 'Invalid is_active value'
});
}
const printer = await Printer.findById(parseInt(printer_id, 10));
if (!printer) {
return res.status(404).json({
success: false,
message: 'Printer not found'
});
}
await Printer.toggleActive(parseInt(printer_id, 10), is_active);
res.json({
success: true,
message: `Printer ${is_active ? 'activated' : 'deactivated'}`,
data: {
printer_id: parseInt(printer_id, 10),
is_active
}
});
} catch (error) {
console.error('Error toggling printer active:', error);
res.status(500).json({
success: false,
message: 'Failed to update printer'
});
}
}
module.exports = {
syncPrinters,
getAllPrinters,
updatePrinterStatus,
assignPrinterToDepartment,
togglePrinterActive
};