import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
import {PrintService} from '@eg/share/print/print.service';
+interface CheckoutStat {
+ override?: boolean;
+ redo?: boolean;
+ renew?: boolean;
+}
+
@Injectable({providedIn: 'root'})
export class SckoService {
private patrons: PatronService,
) {}
+ logoutStaff() {
+ this.resetPatron();
+ this.auth.logout();
+ this.router.navigate(['/scko']);
+ }
+
resetPatron() {
this.patronSummary = null;
this.sessionCheckouts = [];
}
- /*
- logout(receiptType:
- this.printer.print({
- templateName: 'bills_current',
- contextData: {xacts: rows},
- printContext: 'default'
- });
- */
+ checkout(barcode: string, override?: boolean): Promise<any> {
+ this.resetPatronTimeout();
+
+ let method = 'open-ils.circ.checkout.full';
+ if (override) { method += '.override'; }
+
+ return this.net.request(
+ 'open-ils.circ', method, this.auth.token(), {
+ patron_id: this.patronSummary.id,
+ copy_barcode: barcode
+ }).toPromise()
+
+ .then(result => {
+
+ console.debug('CO returned', result);
+
+ return this.handleCheckoutResult(result, barcode, 'checkout');
+
+ }).then(stat => {
+ console.debug('handleCheckoutResult returned', stat);
+
+ if (stat.override) {
+ return this.checkout(barcode, true);
+ } else if (stat.redo) {
+ return this.checkout(barcode);
+ } else if (stat.renew) {
+ return this.renew(barcode);
+ }
+ });
+
+ // return this.router.navigate(['/scko']);
+ }
+
+ renew(barcode: string, override?: boolean): Promise<any> {
+
+ let method = 'open-ils.circ.renew';
+ if (override) { method += '.override'; }
+
+ return this.net.request(
+ 'open-ils.circ', method, this.auth.token(), {
+ patron_id: this.patronSummary.id,
+ copy_barcode: barcode
+ }).toPromise()
+
+ .then(result => {
+ console.debug('Renew returned', result);
+ return this.handleCheckoutResult(result, barcode, 'renew');
+
+ }).then(stat => {
+ console.debug('handleCheckoutResult returned', stat);
+
+ if (stat.override) {
+ return this.renew(barcode, true);
+ }
+ });
+ }
+
+
+ handleCheckoutResult(result: any,
+ barcode: string, action: string): Promise<CheckoutStat> {
+
+ if (Array.isArray(result)) {
+ result = result[0];
+ }
+
+ const evt: any = this.evt.parse(result) || {};
+ const payload = evt.payload || {};
+
+ if (evt.textcode === 'NO_SESSION') {
+ this.logoutStaff();
+ return;
+ }
+
+ if (evt.textcode === 'SUCCESS') {
+
+ if (action === 'checkout') {
+
+ return Promise.resolve({});
+
+ } else if (action === 'renew') {
+
+ return Promise.resolve({});
+ }
+ }
+
+ return Promise.resolve({}); // TODO
+ }
}