Skip to content

Commit

Permalink
fix: send print request to api in correct format
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHulme committed May 31, 2024
1 parent f114adc commit a5d7fbd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
25 changes: 5 additions & 20 deletions src/api/PrintMyBarcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,11 @@ const createLabels = (barcodes) => {
.replace(/ /g, '-')
.toUpperCase()

// {
// "print_job": {
// "printer_name": "stub",
// "label_template_name": "traction_tube_label_template",
// "labels": [{
// "barcode": "TRAC-2-4-N1",
// "first_line": "15-Sep-22",
// "second_line": "TRAC-2-4-N1",
// "third_line": "",
// "label_name": "main_label"
// }],
// "copies": "1"
// }
// }
return barcodes.map((barcode) => ({
label_name: 'main_label',
top_left: dateString,
bottom_left: barcode,
barcode: barcode,
label_name: 'main_label',
}))
}

Expand Down Expand Up @@ -60,11 +46,10 @@ const createPrintJob = async ({ printer, barcodes }) => {

const labels = createLabels(barcodes)
const data = {
print_job: {
printer_name: printer,
label_template_name: import.meta.env.VITE_LABEL_TEMPLATE_NAME,
labels,
},
printer_name: printer,
label_template_name: import.meta.env.VITE_LABEL_TEMPLATE_NAME,
labels,
copies: '1',
}
return fetch(
`${import.meta.env.VITE_PRINT_MY_BARCODE_BASE_URL}/v2/print_jobs`,
Expand Down
28 changes: 16 additions & 12 deletions src/components/PrintJob.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,23 @@ export default {
createPrintJob({
printer: this.printer,
barcodes: this.formattedBarcodes(),
}).then((response) => {
if (!response.ok) {
response.json().then((data) => {
const message = data.errors
.map((error) => `${error.source.pointer} ${error.detail}`)
.join(', ')
this.showAlert('Barcode printing failed: ' + message, 'danger')
})
} else {
this.showAlert('Barcode printing succeeded', 'success')
}
return response
})
.then((response) => {
if (!response.ok) {
response.json().then((data) => {
const message = data.errors
.map((error) => `${error.source.pointer} ${error.detail}`)
.join(', ')
this.showAlert('Barcode printing failed: ' + message, 'danger')
})
} else {
this.showAlert('Barcode printing succeeded', 'success')
}
return response
})
.catch((error) => {
this.showAlert('Barcode printing failed: ' + error.message, 'danger')
})
},
valid() {
this.barcodeError = !this.barcodes
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/api/PrintMyBarcode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ describe('PrintMyBarcode.js', () => {
Accept: 'application/vnd.api+json',
},
body: JSON.stringify({
print_job: {
printer_name: printer,
label_template_name: 'sqsc_96plate_label_template_code128',
labels: createLabels(barcodes),
},
printer_name: printer,
label_template_name: 'sqsc_96plate_label_template_code128',
labels: createLabels(barcodes),
copies: '1',
}),
},
)
Expand Down

0 comments on commit a5d7fbd

Please sign in to comment.