diff --git a/@types/global/index.d.ts b/@types/global/index.d.ts index c76e5c4..8671bda 100644 --- a/@types/global/index.d.ts +++ b/@types/global/index.d.ts @@ -25,9 +25,9 @@ declare global { function readOrder(id: string): Promise; - function deleteOrder(id: string): Promise; + function deleteOrder(id: string): Promise; - function deleteOrders(ids: string[]): Promise; + function deleteOrders(ids: string[]): Promise; function readMenuItems(): Promise; } diff --git a/api/lib/io.js b/api/lib/io.js index fbea820..f641d54 100644 --- a/api/lib/io.js +++ b/api/lib/io.js @@ -2,7 +2,7 @@ const fs = require("fs"); const {Notification} = require('electron'); const os = require("os"); const readline = require('readline'); -const {uuidv4, removeElement} = require('./commons'); +const {uuidv4, removeElement, updateElement} = require('./commons'); const platesFilePath = `${os.homedir()}/Documents/plate-config.txt`; const ordersFilePath = `${os.homedir()}/Documents/orders.txt`; const menuItemsFilePath = `${os.homedir()}/Documents/menu-items.txt`; @@ -74,8 +74,9 @@ async function deletePlateById(_, id) { } if (foundIndex !== undefined) { - removeElement(lines, foundIndex) - fs.writeFile(platesFilePath, lines, + lines.splice(foundIndex, 1); + let linesAsString = lines.join("\r\n"); + fs.writeFile(platesFilePath, linesAsString, function (err, data) { if (err) { return Promise.reject("Fail to write config file: " + err); @@ -90,8 +91,43 @@ async function deletePlateById(_, id) { } } -function updatePlate(_, config) { - // todo +async function updatePlate(_, config) { + try { + let foundIndex = undefined; + + await fs.readFile(platesFilePath, 'utf8', function (err, data) { + if (err) { + return Promise.reject(err); + } + + let lines = data.split('\r\n'); + for (let index = 0; index < lines.length; index++) { + const line = lines[index]; + if (line !== "" && line.includes(`"_id":"${config._id}"`)) { + foundIndex = index; + break; + } + } + + if (foundIndex !== undefined) { + lines[foundIndex] = `${JSON.stringify(config)}`; + let linesAsString = lines.join("\r\n"); + fs.writeFile(platesFilePath, linesAsString, + function (err, data) { + if (err) { + return Promise.reject("Fail to write config file: " + err); + } + + }); + } else { + return Promise.resolve("Config not found"); + } + }); + + return Promise.resolve(config); + } catch (e) { + return Promise.reject(e); + } } function _getReaderInterface(filePath) { @@ -115,8 +151,6 @@ const filesAdded = () => { async function addOrder(_, order) { order._id = uuidv4(); const row = `${JSON.stringify(order)}\r\n`; - console.log(row); - await fs.appendFile(ordersFilePath, row, err => { if (err) { return Promise.resolve("Error!"); @@ -136,8 +170,7 @@ async function addOrders(_, orders) { } // display notification - // filesAdded(); - return Promise.resolve("Saved!"); + return Promise.resolve(orders); } async function readOrders(_) { @@ -190,33 +223,101 @@ async function deleteOrderById(_, id) { } if (foundIndex !== undefined) { - removeElement(lines, foundIndex) - fs.writeFile(ordersFilePath, lines, + lines.splice(foundIndex, 1); + let linesAsString = lines.join("\r\n"); + fs.writeFile(ordersFilePath, linesAsString, function (err, data) { if (err) { - return Promise.reject("Fail to write config file: " + err); + return Promise.reject("Fail to write order file: " + err); } }); } else { - return Promise.resolve("Plate configuration not found"); + return Promise.resolve("Order not found"); } }); + + return Promise.resolve(true); } catch (e) { return Promise.reject(e); } } async function deleteOrdersByIds(_, ids) { - for (const idKey in ids) { - const id = ids[idKey]; - await deleteOrderById(_, id); - } + try { + await fs.readFile(ordersFilePath, 'utf8', function (err, data) { + if (err) { + return Promise.reject(err); + } + let foundIndex = undefined; + let lines = data.split('\r\n'); + + for (let j = 0; j < ids.length; j++) { + const id = ids[j]; + + for (let index = 0; index < lines.length; index++) { + const line = lines[index]; + if (line !== "" && line.includes(`"_id":"${id}"`)) { + foundIndex = index; + break; + } + } + if (foundIndex !== undefined) { + lines.splice(foundIndex, 1); + } + } - return Promise.resolve("Deleted!"); + let linesAsString = lines.join("\r\n"); + fs.writeFile(ordersFilePath, linesAsString, + function (err, data) { + if (err) { + return Promise.reject("Fail to write order file: " + err); + } + }); + }); + + return Promise.resolve(true); + } catch (e) { + return Promise.reject(e); + } } -function updateOrder(_, order) { - // todo +async function updateOrder(_, order) { + try { + let foundIndex = undefined; + + await fs.readFile(ordersFilePath, 'utf8', function (err, data) { + if (err) { + return Promise.reject(err); + } + + let lines = data.split('\r\n'); + for (let index = 0; index < lines.length; index++) { + const line = lines[index]; + if (line !== "" && line.includes(`"_id":"${order._id}"`)) { + foundIndex = index; + break; + } + } + + if (foundIndex !== undefined) { + lines[foundIndex] = `${JSON.stringify(order)}`; + let linesAsString = lines.join("\r\n"); + fs.writeFile(ordersFilePath, linesAsString, + function (err, data) { + if (err) { + return Promise.reject("Fail to write order file: " + err); + } + + }); + } else { + return Promise.resolve("Order not found"); + } + }); + + return Promise.resolve(order); + } catch (e) { + return Promise.reject(e); + } } async function readMenuItems(_) { diff --git a/src/app/modules/orders/order-new/order-new.component.html b/src/app/modules/orders/order-new/order-new.component.html index 2a54ace..e1fe877 100644 --- a/src/app/modules/orders/order-new/order-new.component.html +++ b/src/app/modules/orders/order-new/order-new.component.html @@ -31,14 +31,14 @@

Insertion form

formControlName="quantity" > -
- - -
+
+ + +
+
+ + +
diff --git a/src/app/modules/orders/orders/orders.component.ts b/src/app/modules/orders/orders/orders.component.ts index 2d22b24..0713191 100644 --- a/src/app/modules/orders/orders/orders.component.ts +++ b/src/app/modules/orders/orders/orders.component.ts @@ -91,9 +91,12 @@ export class OrdersComponent implements OnInit, OnDestroy { header: 'Confirm', icon: 'pi pi-exclamation-triangle', accept: () => { - this.orders = this.orders.filter(val => !this.selectedOrders.includes(val)); - this.selectedOrders = []; - this.messageService.add({severity:'success', summary: 'Successful', detail: 'Orders Deleted', life: 3000}); + const ids = this.selectedOrders.map(item => item._id!); + this.apiConnector.removeOrders(ids).subscribe(() => { + this.orders = this.orders.filter(val => !this.selectedOrders.includes(val)); + this.selectedOrders = []; + this.messageService.add({severity:'success', summary: 'Successful', detail: 'Orders Deleted', life: 3000}); + }) } }); } @@ -111,7 +114,8 @@ export class OrdersComponent implements OnInit, OnDestroy { category: this.currentOrder.menuItem.parent.data }; const currentOrderIdx = this.orders.findIndex(order => order._id === this.currentOrder._id); - this.orders[currentOrderIdx] = { + + const editOrder = { ... this.orders[currentOrderIdx], orderId: this.currentOrder.orderId, menuItem, @@ -120,9 +124,19 @@ export class OrdersComponent implements OnInit, OnDestroy { date: this.currentOrder.date, plate: this.currentOrder.plate }; + this.apiConnector.updateOrder(editOrder).subscribe(order => { + console.log("order" + order); - // todo call update - + this.orders[currentOrderIdx] = { + ... this.orders[currentOrderIdx], + orderId: order.orderId, + menuItem, + status: order.status, + notes: order.notes, + date: order.date, + plate: order.plate + }; + }); } else { // new orders let newOrders: Order[] = []; @@ -138,16 +152,16 @@ export class OrdersComponent implements OnInit, OnDestroy { _id: this.ordersService.createId(), orderId: this.currentOrder.orderId, menuItem, - status: Status.Todo, + status: this.statuses[0], notes: this.currentOrder.notes, date: dateFormatted, plate: this.currentOrder.plate }); } - // todo call save - - this.orders = this.orders.concat(newOrders); + this.apiConnector.addOrders(newOrders).subscribe(orders => { + this.orders = this.orders.concat(orders); + }) } this.orderDialog = false; @@ -180,6 +194,16 @@ export class OrdersComponent implements OnInit, OnDestroy { } deleteOrder(order: Order) { - // todo + this.confirmationService.confirm({ + message: 'Are you sure you want to delete the selected order?', + header: 'Confirm', + icon: 'pi pi-exclamation-triangle', + accept: () => { + this.apiConnector.removeOrder(order._id!).subscribe(() => { + this.orders = this.orders.filter(val => val != order); + this.messageService.add({severity:'success', summary: 'Successful', detail: 'Orders Deleted', life: 3000}); + }) + } + }); } } diff --git a/src/app/services/file-system-connector.service.ts b/src/app/services/file-system-connector.service.ts index ff19117..f4b5b29 100644 --- a/src/app/services/file-system-connector.service.ts +++ b/src/app/services/file-system-connector.service.ts @@ -83,11 +83,11 @@ export class FileSystemConnectorService implements ApiConnector { return fromPromise(fs.readOrders()); } - removeOrder(id: string): Observable { + removeOrder(id: string): Observable { return fromPromise(fs.deleteOrder(id)); } - removeOrders(ids: string[]): Observable { + removeOrders(ids: string[]): Observable { return fromPromise(fs.deleteOrders(ids)); } diff --git a/src/app/services/http-rest-connector.service.ts b/src/app/services/http-rest-connector.service.ts index d3bb2f5..ee65cec 100644 --- a/src/app/services/http-rest-connector.service.ts +++ b/src/app/services/http-rest-connector.service.ts @@ -43,10 +43,11 @@ export class HttpRestConnectorService implements ApiConnector { } addOrders(orders: Order[]): Observable { + // todo temporary, used for mocked data return of(true); } - getOrder(id: string): Observable { + getOrder(id: string): Observable { return new Observable(); } @@ -58,19 +59,22 @@ export class HttpRestConnectorService implements ApiConnector { ); } - removeOrder(id: string): Observable { - return new Observable(); + removeOrder(id: string): Observable { + // todo temporary, used for mocked data + return of(true); } - removeOrders(ids: string[]): Observable { - return new Observable(); + removeOrders(ids: string[]): Observable { + // todo temporary, used for mocked data + return of(true); } - updateOrder(order: Order): Observable { - return new Observable(); + updateOrder(order: Order): Observable { + // todo temporary, used for mocked data + return of(order); } - getMenuItems(): Observable { + getMenuItems(): Observable { // todo temporary, used for mocked data return this.http.get('assets/menu-items.json') .pipe( diff --git a/src/assets/orders.json b/src/assets/orders.json index 884899d..c287ccf 100644 --- a/src/assets/orders.json +++ b/src/assets/orders.json @@ -13,7 +13,7 @@ } }, "orderId": 1, - "status": "todo", + "status": {"label":"Todo","value":"todo"}, "date": "2022-07-04 11:00:00" }, { @@ -29,11 +29,8 @@ } }, "orderId": 2, - "plate": { - "_id": "1c7ca4c9-b6d2-43b4-b83e-d30023e8a2e7", - "name": "plate 1" - }, - "status": "progress", + "plate": {"name":"piastra 2","code":"c55ffbb0-e60f-422a-a024-86a5048c5fb4"}, + "status": {"label":"Todo","value":"todo"}, "date": "2022-07-04 11:01:00" }, { @@ -49,11 +46,8 @@ } }, "orderId": 3, - "plate": { - "_id": "c55ffbb0-e60f-422a-a024-86a5048c5fb4", - "name": "plate 2" - }, - "status": "progress", + "plate": {"name":"piastra 2","code":"c55ffbb0-e60f-422a-a024-86a5048c5fb4"}, + "status": {"label":"Progress","value":"progress"}, "date": "2022-07-04 11:02:00" }, { @@ -69,7 +63,7 @@ } }, "orderId": 4, - "status": "done", + "status": {"label":"Done","value":"done"}, "date": "2022-07-04 10:55:00" } ]