diff --git a/.babelrc b/.babelrc index 1320b9a3..eeeb0e93 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,8 @@ { - "presets": ["@babel/preset-env"] -} + "presets": [ + "@babel/preset-env" + ], + "plugins": [ + "@babel/plugin-proposal-class-properties" + ] +} \ No newline at end of file diff --git a/.env b/.env new file mode 100644 index 00000000..df0c9c2d --- /dev/null +++ b/.env @@ -0,0 +1 @@ +ANALISYS_DAYS_INTERVAL=30 \ No newline at end of file diff --git a/index.js b/index.js index 525c9de1..19325b68 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,7 @@ -import { Drug, Pharmacy } from "./pharmacy"; -import fs from "fs"; +"use strict"; -const drugs = [ - new Drug("Doliprane", 20, 30), - new Drug("Herbal Tea", 10, 5), - new Drug("Fervex", 5, 40), - new Drug("Magic Pill", 15, 40) -]; -const trial = new Pharmacy(drugs); +import { items } from "./src/data"; +import { Manage } from "./src/services/manage"; -const log = []; - -for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) { - log.push(JSON.stringify(trial.updateBenefitValue())); -} - -/* eslint-disable no-console */ -fs.writeFile("output.txt", log.toString(), err => { - if (err) { - console.log("error"); - } else { - console.log("success"); - } -}); -/* eslint-enable no-console */ +const manage = new Manage(items); +manage.process(); diff --git a/package-lock.json b/package-lock.json index bc5577d3..0cff30a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@babel/cli": "^7.1.0", "@babel/core": "^7.1.0", "@babel/node": "^7.0.0", + "@babel/plugin-proposal-class-properties": "^7.16.7", "@babel/preset-env": "^7.1.0", "babel-core": "^7.0.0-bridge.0", "babel-eslint": "^9.0.0", @@ -154,12 +155,15 @@ } }, "node_modules/@babel/helper-annotate-as-pure": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0.tgz", - "integrity": "sha512-3UYcJUj9kvSLbLbUIfQTqzcy5VX7GRZ/CCDrnOaZorFFM01aXp1+GJwuFGV4NDDoAS+mOUyHcO6UD/RfqOks3Q==", + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.16.7.tgz", + "integrity": "sha512-s6t2w/IPQVTAET1HitoowRGXooX8mCgtuP5195wD/QJPV6wYjpujCGF7JuMODVX2ZAJOf1GT6DT9MHEZvLOFSw==", "dev": true, "dependencies": { - "@babel/types": "^7.0.0" + "@babel/types": "^7.16.7" + }, + "engines": { + "node": ">=6.9.0" } }, "node_modules/@babel/helper-builder-binary-assignment-operator-visitor": { @@ -210,6 +214,27 @@ "semver": "bin/semver.js" } }, + "node_modules/@babel/helper-create-class-features-plugin": { + "version": "7.17.9", + "resolved": "https://registry.npmjs.org/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.17.9.tgz", + "integrity": "sha512-kUjip3gruz6AJKOq5i3nC6CoCEEF/oHH3cp6tOZhB+IyyyPyW0g1Gfsxn3mkk6S08pIA2y8GQh609v9G/5sHVQ==", + "dev": true, + "dependencies": { + "@babel/helper-annotate-as-pure": "^7.16.7", + "@babel/helper-environment-visitor": "^7.16.7", + "@babel/helper-function-name": "^7.17.9", + "@babel/helper-member-expression-to-functions": "^7.17.7", + "@babel/helper-optimise-call-expression": "^7.16.7", + "@babel/helper-replace-supers": "^7.16.7", + "@babel/helper-split-export-declaration": "^7.16.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0" + } + }, "node_modules/@babel/helper-define-map": { "version": "7.5.5", "resolved": "https://registry.npmjs.org/@babel/helper-define-map/-/helper-define-map-7.5.5.tgz", @@ -244,14 +269,13 @@ } }, "node_modules/@babel/helper-function-name": { - "version": "7.16.7", - "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.16.7.tgz", - "integrity": "sha512-QfDfEnIUyyBSR3HtrtGECuZ6DAyCkYFp7GHl75vFtTnn6pjKeK0T1DB5lLkFvBea8MdaiUABx3osbgLyInoejA==", + "version": "7.17.9", + "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.17.9.tgz", + "integrity": "sha512-7cRisGlVtiVqZ0MW0/yFB4atgpGLWEHUVYnb448hZK4x+vih0YO5UoS11XIYtZYqHd0dIPMdUSv8q5K4LdMnIg==", "dev": true, "dependencies": { - "@babel/helper-get-function-arity": "^7.16.7", "@babel/template": "^7.16.7", - "@babel/types": "^7.16.7" + "@babel/types": "^7.17.0" }, "engines": { "node": ">=6.9.0" @@ -282,12 +306,15 @@ } }, "node_modules/@babel/helper-member-expression-to-functions": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.5.5.tgz", - "integrity": "sha512-5qZ3D1uMclSNqYcXqiHoA0meVdv+xUEex9em2fqMnrk/scphGlGgg66zjMrPJESPwrFJ6sbfFQYUSa0Mz7FabA==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.17.7.tgz", + "integrity": "sha512-thxXgnQ8qQ11W2wVUObIqDL4p148VMxkt5T/qpN5k2fboRyzFGFmKsTGViquyM5QHKUy48OZoca8kw4ajaDPyw==", "dev": true, "dependencies": { - "@babel/types": "^7.5.5" + "@babel/types": "^7.17.0" + }, + "engines": { + "node": ">=6.9.0" } }, "node_modules/@babel/helper-module-imports": { @@ -322,12 +349,15 @@ } }, "node_modules/@babel/helper-optimise-call-expression": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.0.0.tgz", - "integrity": "sha512-u8nd9NQePYNQV8iPWu/pLLYBqZBa4ZaY1YWRFMuxrid94wKI1QNt67NEZ7GAe5Kc/0LLScbim05xZFWkAdrj9g==", + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.16.7.tgz", + "integrity": "sha512-EtgBhg7rd/JcnpZFXpBy0ze1YRfdm7BnBX4uKMBd3ixa3RGAE002JZB66FJyNH7g0F38U05pXmA5P8cBh7z+1w==", "dev": true, "dependencies": { - "@babel/types": "^7.0.0" + "@babel/types": "^7.16.7" + }, + "engines": { + "node": ">=6.9.0" } }, "node_modules/@babel/helper-plugin-utils": { @@ -362,15 +392,19 @@ } }, "node_modules/@babel/helper-replace-supers": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.5.5.tgz", - "integrity": "sha512-XvRFWrNnlsow2u7jXDuH4jDDctkxbS7gXssrP4q2nUD606ukXHRvydj346wmNg+zAgpFx4MWf4+usfC93bElJg==", + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.16.7.tgz", + "integrity": "sha512-y9vsWilTNaVnVh6xiJfABzsNpgDPKev9HnAgz6Gb1p6UUwf9NepdlsV7VXGCftJM+jqD5f7JIEubcpLjZj5dBw==", "dev": true, "dependencies": { - "@babel/helper-member-expression-to-functions": "^7.5.5", - "@babel/helper-optimise-call-expression": "^7.0.0", - "@babel/traverse": "^7.5.5", - "@babel/types": "^7.5.5" + "@babel/helper-environment-visitor": "^7.16.7", + "@babel/helper-member-expression-to-functions": "^7.16.7", + "@babel/helper-optimise-call-expression": "^7.16.7", + "@babel/traverse": "^7.16.7", + "@babel/types": "^7.16.7" + }, + "engines": { + "node": ">=6.9.0" } }, "node_modules/@babel/helper-simple-access": { @@ -501,6 +535,22 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/plugin-proposal-class-properties": { + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.16.7.tgz", + "integrity": "sha512-IobU0Xme31ewjYOShSIqd/ZGM/r/cuOz2z0MDbNrhF5FW+ZVgi0f2lyeoj9KFPDOAqsYxmLWZte1WOwlvY9aww==", + "dev": true, + "dependencies": { + "@babel/helper-create-class-features-plugin": "^7.16.7", + "@babel/helper-plugin-utils": "^7.16.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/plugin-proposal-dynamic-import": { "version": "7.5.0", "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.5.0.tgz", @@ -9148,12 +9198,12 @@ } }, "@babel/helper-annotate-as-pure": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0.tgz", - "integrity": "sha512-3UYcJUj9kvSLbLbUIfQTqzcy5VX7GRZ/CCDrnOaZorFFM01aXp1+GJwuFGV4NDDoAS+mOUyHcO6UD/RfqOks3Q==", + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.16.7.tgz", + "integrity": "sha512-s6t2w/IPQVTAET1HitoowRGXooX8mCgtuP5195wD/QJPV6wYjpujCGF7JuMODVX2ZAJOf1GT6DT9MHEZvLOFSw==", "dev": true, "requires": { - "@babel/types": "^7.0.0" + "@babel/types": "^7.16.7" } }, "@babel/helper-builder-binary-assignment-operator-visitor": { @@ -9197,6 +9247,21 @@ } } }, + "@babel/helper-create-class-features-plugin": { + "version": "7.17.9", + "resolved": "https://registry.npmjs.org/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.17.9.tgz", + "integrity": "sha512-kUjip3gruz6AJKOq5i3nC6CoCEEF/oHH3cp6tOZhB+IyyyPyW0g1Gfsxn3mkk6S08pIA2y8GQh609v9G/5sHVQ==", + "dev": true, + "requires": { + "@babel/helper-annotate-as-pure": "^7.16.7", + "@babel/helper-environment-visitor": "^7.16.7", + "@babel/helper-function-name": "^7.17.9", + "@babel/helper-member-expression-to-functions": "^7.17.7", + "@babel/helper-optimise-call-expression": "^7.16.7", + "@babel/helper-replace-supers": "^7.16.7", + "@babel/helper-split-export-declaration": "^7.16.7" + } + }, "@babel/helper-define-map": { "version": "7.5.5", "resolved": "https://registry.npmjs.org/@babel/helper-define-map/-/helper-define-map-7.5.5.tgz", @@ -9228,14 +9293,13 @@ } }, "@babel/helper-function-name": { - "version": "7.16.7", - "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.16.7.tgz", - "integrity": "sha512-QfDfEnIUyyBSR3HtrtGECuZ6DAyCkYFp7GHl75vFtTnn6pjKeK0T1DB5lLkFvBea8MdaiUABx3osbgLyInoejA==", + "version": "7.17.9", + "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.17.9.tgz", + "integrity": "sha512-7cRisGlVtiVqZ0MW0/yFB4atgpGLWEHUVYnb448hZK4x+vih0YO5UoS11XIYtZYqHd0dIPMdUSv8q5K4LdMnIg==", "dev": true, "requires": { - "@babel/helper-get-function-arity": "^7.16.7", "@babel/template": "^7.16.7", - "@babel/types": "^7.16.7" + "@babel/types": "^7.17.0" } }, "@babel/helper-get-function-arity": { @@ -9257,12 +9321,12 @@ } }, "@babel/helper-member-expression-to-functions": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.5.5.tgz", - "integrity": "sha512-5qZ3D1uMclSNqYcXqiHoA0meVdv+xUEex9em2fqMnrk/scphGlGgg66zjMrPJESPwrFJ6sbfFQYUSa0Mz7FabA==", + "version": "7.17.7", + "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.17.7.tgz", + "integrity": "sha512-thxXgnQ8qQ11W2wVUObIqDL4p148VMxkt5T/qpN5k2fboRyzFGFmKsTGViquyM5QHKUy48OZoca8kw4ajaDPyw==", "dev": true, "requires": { - "@babel/types": "^7.5.5" + "@babel/types": "^7.17.0" } }, "@babel/helper-module-imports": { @@ -9291,12 +9355,12 @@ } }, "@babel/helper-optimise-call-expression": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.0.0.tgz", - "integrity": "sha512-u8nd9NQePYNQV8iPWu/pLLYBqZBa4ZaY1YWRFMuxrid94wKI1QNt67NEZ7GAe5Kc/0LLScbim05xZFWkAdrj9g==", + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.16.7.tgz", + "integrity": "sha512-EtgBhg7rd/JcnpZFXpBy0ze1YRfdm7BnBX4uKMBd3ixa3RGAE002JZB66FJyNH7g0F38U05pXmA5P8cBh7z+1w==", "dev": true, "requires": { - "@babel/types": "^7.0.0" + "@babel/types": "^7.16.7" } }, "@babel/helper-plugin-utils": { @@ -9328,15 +9392,16 @@ } }, "@babel/helper-replace-supers": { - "version": "7.5.5", - "resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.5.5.tgz", - "integrity": "sha512-XvRFWrNnlsow2u7jXDuH4jDDctkxbS7gXssrP4q2nUD606ukXHRvydj346wmNg+zAgpFx4MWf4+usfC93bElJg==", + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.16.7.tgz", + "integrity": "sha512-y9vsWilTNaVnVh6xiJfABzsNpgDPKev9HnAgz6Gb1p6UUwf9NepdlsV7VXGCftJM+jqD5f7JIEubcpLjZj5dBw==", "dev": true, "requires": { - "@babel/helper-member-expression-to-functions": "^7.5.5", - "@babel/helper-optimise-call-expression": "^7.0.0", - "@babel/traverse": "^7.5.5", - "@babel/types": "^7.5.5" + "@babel/helper-environment-visitor": "^7.16.7", + "@babel/helper-member-expression-to-functions": "^7.16.7", + "@babel/helper-optimise-call-expression": "^7.16.7", + "@babel/traverse": "^7.16.7", + "@babel/types": "^7.16.7" } }, "@babel/helper-simple-access": { @@ -9434,6 +9499,16 @@ "@babel/plugin-syntax-async-generators": "^7.2.0" } }, + "@babel/plugin-proposal-class-properties": { + "version": "7.16.7", + "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.16.7.tgz", + "integrity": "sha512-IobU0Xme31ewjYOShSIqd/ZGM/r/cuOz2z0MDbNrhF5FW+ZVgi0f2lyeoj9KFPDOAqsYxmLWZte1WOwlvY9aww==", + "dev": true, + "requires": { + "@babel/helper-create-class-features-plugin": "^7.16.7", + "@babel/helper-plugin-utils": "^7.16.7" + } + }, "@babel/plugin-proposal-dynamic-import": { "version": "7.5.0", "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.5.0.tgz", diff --git a/package.json b/package.json index 93cdff60..8e60d8c4 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@babel/cli": "^7.1.0", "@babel/core": "^7.1.0", "@babel/node": "^7.0.0", + "@babel/plugin-proposal-class-properties": "^7.16.7", "@babel/preset-env": "^7.1.0", "babel-core": "^7.0.0-bridge.0", "babel-eslint": "^9.0.0", diff --git a/pharmacy.js b/pharmacy.js index cda44c41..875817c9 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -1,3 +1,5 @@ +"use strict"; + export class Drug { constructor(name, expiresIn, benefit) { this.name = name; @@ -11,54 +13,58 @@ export class Pharmacy { this.drugs = drugs; } updateBenefitValue() { + let name; + let benefit; + let expiresIn; for (var i = 0; i < this.drugs.length; i++) { - if ( - this.drugs[i].name != "Herbal Tea" && - this.drugs[i].name != "Fervex" - ) { - if (this.drugs[i].benefit > 0) { - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].benefit = this.drugs[i].benefit - 1; + name = this.drugs[i].name; + benefit = this.drugs[i].benefit; + expiresIn = this.drugs[i].expiresIn; + + // "Magic Pill" never expires nor decreases in Benefit + if (name != "Magic Pill") { + if ( + name != "Herbal Tea" && + name != "Fervex" + ) { + if (benefit > 0) { + benefit--; + if (name == "Dafalgan") { + benefit--; + } } - } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - if (this.drugs[i].name == "Fervex") { - if (this.drugs[i].expiresIn < 11) { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; + } else { + if (benefit < 50) { + benefit++; + if (name == "Fervex") { + if (expiresIn < 11) { + benefit++; } - } - if (this.drugs[i].expiresIn < 6) { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; + if (expiresIn < 6) { + benefit++; } } } } - } - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1; - } - if (this.drugs[i].expiresIn < 0) { - if (this.drugs[i].name != "Herbal Tea") { - if (this.drugs[i].name != "Fervex") { - if (this.drugs[i].benefit > 0) { - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].benefit = this.drugs[i].benefit - 1; - } + + if (expiresIn > 0) expiresIn--; + + if (expiresIn < 0) { + if (name == "Herbal Tea") { + if (benefit < 50) { + benefit = benefit + 2; } - } else { - this.drugs[i].benefit = - this.drugs[i].benefit - this.drugs[i].benefit; } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; + + if (name == "Fervex") { + benefit = 0 } } } + + this.drugs[i].name = name; + this.drugs[i].expiresIn = expiresIn; + this.drugs[i].benefit = benefit; } return this.drugs; diff --git a/src/data.js b/src/data.js new file mode 100644 index 00000000..6c4ea749 --- /dev/null +++ b/src/data.js @@ -0,0 +1,29 @@ +"use strict"; + +export const items = [ + { + name: "Doliprane", + expiresIn: 20, + benefit: 30 + }, + { + name: "Herbal Tea", + expiresIn: 10, + benefit: 5 + }, + { + name: "Fervex", + expiresIn: 5, + benefit: 40 + }, + { + name: "Magic Pill", + expiresIn: 15, + benefit: 40 + }, + { + name: "Dafalgan", + expiresIn: 15, + benefit: 20 + }, +]; \ No newline at end of file diff --git a/src/helpers/logger.js b/src/helpers/logger.js new file mode 100644 index 00000000..b673f75b --- /dev/null +++ b/src/helpers/logger.js @@ -0,0 +1,32 @@ +"use strict"; + +import fs from "fs"; + +class LoggerError extends Error { + constructor(error) { + super(); + this.message = error; + } +} + +export class Logger { + _log; + + pushLog(log) { + this._log = log; + } + + writeLog() { + try { + fs.writeFile("output.txt", this._log.toString(), err => { + if (err) { + console.log("error"); + } else { + console.log("success"); + } + }); + } catch (e) { + return new LoggerError(e); + } + } +} diff --git a/src/helpers/logger.spec.js b/src/helpers/logger.spec.js new file mode 100644 index 00000000..e8586536 --- /dev/null +++ b/src/helpers/logger.spec.js @@ -0,0 +1,35 @@ +"use strict"; + +import { Logger } from "./logger"; + +describe("Logger helper", () => { + let service; + + const mockLog = [{ + name: "Doliprane", + expiresIn: 20, + benefit: 30 + }]; + + beforeEach(() => { + service = new Logger(); + service._log = [] + }); + + describe("#pushLog", () => { + it("should push a log", () => { + service.pushLog(mockLog); + expect(service._log).toEqual(mockLog); + }); + }); + + describe("#writeLog", () => { + beforeEach(() => { + service._log = mockLog; + }); + it("Job is fulfilled", () => { + expect(service.writeLog).toBeTruthy(); + }); + }); + +}); \ No newline at end of file diff --git a/src/services/manage.js b/src/services/manage.js new file mode 100644 index 00000000..b075da33 --- /dev/null +++ b/src/services/manage.js @@ -0,0 +1,38 @@ +"use strict"; + +import { Drug, Pharmacy } from "../../pharmacy"; +import { Logger } from "../helpers/logger"; + +class ManageError extends Error { + constructor(error) { + super(); + this.message = error; + } +} + +export class Manage extends Logger { + _pharmacy; + + constructor(items) { + super(); + this._buildinventory(items) + } + + _buildinventory(items) { + const drugs = items.map(item => new Drug(item.name, item.expiresIn, item.benefit)); + this._pharmacy = new Pharmacy(drugs); + } + + process() { + const delay = process.env.ANALISYS_DAYS_INTERVAL || 30; + try { + for (let elapsedDays = 0; elapsedDays < delay; elapsedDays++) { + const output = this._pharmacy.updateBenefitValue(); + this.pushLog(JSON.stringify(output)); + } + this.writeLog(); + } catch (e) { + return new ManageError(e); + } + } +} \ No newline at end of file diff --git a/src/services/manage.spec.js b/src/services/manage.spec.js new file mode 100644 index 00000000..104b12b4 --- /dev/null +++ b/src/services/manage.spec.js @@ -0,0 +1,45 @@ +"use strict"; + +import { Manage } from "./manage"; + +describe("Logger helper", () => { + let service; + + const mockDataItems = [ + { + name: "Drug1", + expiresIn: 5, + benefit: 40 + }, + { + name: "Drug2", + expiresIn: 15, + benefit: 40 + } + ]; + + beforeEach(() => { + service = new Manage(mockDataItems); + }); + + describe("On class init", () => { + it("should call #_buildinventory", () => { + expect(service._pharmacy).toBeDefined(); + expect(service._pharmacy.drugs.length).toEqual(2); + expect(service._pharmacy.drugs[0]).toEqual(mockDataItems[0]); + expect(service._pharmacy.drugs[1]).toEqual(mockDataItems[1]); + }); + }); + + describe("#process", () => { + describe("When catching problem", () => { + it("should throw error", () => { + jest.spyOn(service, "pushLog"); + jest.spyOn(service, "writeLog"); + service.process(); + expect(service.pushLog).toHaveBeenCalledTimes(30); + expect(service.writeLog).toHaveBeenCalledTimes(1); + }); + }); + }); +}); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index a191afad..bab6ae72 100644 --- a/yarn.lock +++ b/yarn.lock @@ -68,12 +68,12 @@ "jsesc" "^2.5.1" "source-map" "^0.5.0" -"@babel/helper-annotate-as-pure@^7.0.0": - "integrity" "sha512-3UYcJUj9kvSLbLbUIfQTqzcy5VX7GRZ/CCDrnOaZorFFM01aXp1+GJwuFGV4NDDoAS+mOUyHcO6UD/RfqOks3Q==" - "resolved" "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0.tgz" - "version" "7.0.0" +"@babel/helper-annotate-as-pure@^7.0.0", "@babel/helper-annotate-as-pure@^7.16.7": + "integrity" "sha512-s6t2w/IPQVTAET1HitoowRGXooX8mCgtuP5195wD/QJPV6wYjpujCGF7JuMODVX2ZAJOf1GT6DT9MHEZvLOFSw==" + "resolved" "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.16.7.tgz" + "version" "7.16.7" dependencies: - "@babel/types" "^7.0.0" + "@babel/types" "^7.16.7" "@babel/helper-builder-binary-assignment-operator-visitor@^7.1.0": "integrity" "sha512-qNSR4jrmJ8M1VMM9tibvyRAHXQs2PmaksQF7c1CGJNipfe3D8p+wgNwgso/P2A2r2mdgBWAXljNWR0QRZAMW8w==" @@ -102,6 +102,19 @@ "browserslist" "^4.17.5" "semver" "^6.3.0" +"@babel/helper-create-class-features-plugin@^7.16.7": + "integrity" "sha512-kUjip3gruz6AJKOq5i3nC6CoCEEF/oHH3cp6tOZhB+IyyyPyW0g1Gfsxn3mkk6S08pIA2y8GQh609v9G/5sHVQ==" + "resolved" "https://registry.npmjs.org/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.17.9.tgz" + "version" "7.17.9" + dependencies: + "@babel/helper-annotate-as-pure" "^7.16.7" + "@babel/helper-environment-visitor" "^7.16.7" + "@babel/helper-function-name" "^7.17.9" + "@babel/helper-member-expression-to-functions" "^7.17.7" + "@babel/helper-optimise-call-expression" "^7.16.7" + "@babel/helper-replace-supers" "^7.16.7" + "@babel/helper-split-export-declaration" "^7.16.7" + "@babel/helper-define-map@^7.5.5": "integrity" "sha512-fTfxx7i0B5NJqvUOBBGREnrqbTxRh7zinBANpZXAVDlsZxYdclDp467G1sQ8VZYMnAURY3RpBUAgOYT9GfzHBg==" "resolved" "https://registry.npmjs.org/@babel/helper-define-map/-/helper-define-map-7.5.5.tgz" @@ -126,16 +139,15 @@ "@babel/traverse" "^7.1.0" "@babel/types" "^7.0.0" -"@babel/helper-function-name@^7.1.0", "@babel/helper-function-name@^7.16.7": - "integrity" "sha512-QfDfEnIUyyBSR3HtrtGECuZ6DAyCkYFp7GHl75vFtTnn6pjKeK0T1DB5lLkFvBea8MdaiUABx3osbgLyInoejA==" - "resolved" "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.16.7.tgz" - "version" "7.16.7" +"@babel/helper-function-name@^7.1.0", "@babel/helper-function-name@^7.16.7", "@babel/helper-function-name@^7.17.9": + "integrity" "sha512-7cRisGlVtiVqZ0MW0/yFB4atgpGLWEHUVYnb448hZK4x+vih0YO5UoS11XIYtZYqHd0dIPMdUSv8q5K4LdMnIg==" + "resolved" "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.17.9.tgz" + "version" "7.17.9" dependencies: - "@babel/helper-get-function-arity" "^7.16.7" "@babel/template" "^7.16.7" - "@babel/types" "^7.16.7" + "@babel/types" "^7.17.0" -"@babel/helper-get-function-arity@^7.0.0", "@babel/helper-get-function-arity@^7.16.7": +"@babel/helper-get-function-arity@^7.0.0": "integrity" "sha512-flc+RLSOBXzNzVhcLu6ujeHUrD6tANAOU5ojrRx/as+tbzf8+stUCj7+IfRRoAbEZqj/ahXEMsjhOhgeZsrnTw==" "resolved" "https://registry.npmjs.org/@babel/helper-get-function-arity/-/helper-get-function-arity-7.16.7.tgz" "version" "7.16.7" @@ -149,12 +161,12 @@ dependencies: "@babel/types" "^7.16.7" -"@babel/helper-member-expression-to-functions@^7.5.5": - "integrity" "sha512-5qZ3D1uMclSNqYcXqiHoA0meVdv+xUEex9em2fqMnrk/scphGlGgg66zjMrPJESPwrFJ6sbfFQYUSa0Mz7FabA==" - "resolved" "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.5.5.tgz" - "version" "7.5.5" +"@babel/helper-member-expression-to-functions@^7.16.7", "@babel/helper-member-expression-to-functions@^7.17.7": + "integrity" "sha512-thxXgnQ8qQ11W2wVUObIqDL4p148VMxkt5T/qpN5k2fboRyzFGFmKsTGViquyM5QHKUy48OZoca8kw4ajaDPyw==" + "resolved" "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.17.7.tgz" + "version" "7.17.7" dependencies: - "@babel/types" "^7.5.5" + "@babel/types" "^7.17.0" "@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.16.7": "integrity" "sha512-LVtS6TqjJHFc+nYeITRo6VLXve70xmq7wPhWTqDJusJEgGmkAACWwMiTNrvfoQo6hEhFwAIixNkvB0jPXDL8Wg==" @@ -177,14 +189,14 @@ "@babel/traverse" "^7.16.7" "@babel/types" "^7.16.7" -"@babel/helper-optimise-call-expression@^7.0.0": - "integrity" "sha512-u8nd9NQePYNQV8iPWu/pLLYBqZBa4ZaY1YWRFMuxrid94wKI1QNt67NEZ7GAe5Kc/0LLScbim05xZFWkAdrj9g==" - "resolved" "https://registry.npmjs.org/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.0.0.tgz" - "version" "7.0.0" +"@babel/helper-optimise-call-expression@^7.0.0", "@babel/helper-optimise-call-expression@^7.16.7": + "integrity" "sha512-EtgBhg7rd/JcnpZFXpBy0ze1YRfdm7BnBX4uKMBd3ixa3RGAE002JZB66FJyNH7g0F38U05pXmA5P8cBh7z+1w==" + "resolved" "https://registry.npmjs.org/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.16.7.tgz" + "version" "7.16.7" dependencies: - "@babel/types" "^7.0.0" + "@babel/types" "^7.16.7" -"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.8.0": +"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.16.7", "@babel/helper-plugin-utils@^7.8.0": "integrity" "sha512-Qg3Nk7ZxpgMrsox6HreY1ZNKdBq7K72tDSliA6dCl5f007jR4ne8iD5UzuNnCJH2xBf2BEEVGr+/OL6Gdp7RxA==" "resolved" "https://registry.npmjs.org/@babel/helper-plugin-utils/-/helper-plugin-utils-7.16.7.tgz" "version" "7.16.7" @@ -207,15 +219,16 @@ "@babel/traverse" "^7.1.0" "@babel/types" "^7.0.0" -"@babel/helper-replace-supers@^7.5.5": - "integrity" "sha512-XvRFWrNnlsow2u7jXDuH4jDDctkxbS7gXssrP4q2nUD606ukXHRvydj346wmNg+zAgpFx4MWf4+usfC93bElJg==" - "resolved" "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.5.5.tgz" - "version" "7.5.5" +"@babel/helper-replace-supers@^7.16.7", "@babel/helper-replace-supers@^7.5.5": + "integrity" "sha512-y9vsWilTNaVnVh6xiJfABzsNpgDPKev9HnAgz6Gb1p6UUwf9NepdlsV7VXGCftJM+jqD5f7JIEubcpLjZj5dBw==" + "resolved" "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.16.7.tgz" + "version" "7.16.7" dependencies: - "@babel/helper-member-expression-to-functions" "^7.5.5" - "@babel/helper-optimise-call-expression" "^7.0.0" - "@babel/traverse" "^7.5.5" - "@babel/types" "^7.5.5" + "@babel/helper-environment-visitor" "^7.16.7" + "@babel/helper-member-expression-to-functions" "^7.16.7" + "@babel/helper-optimise-call-expression" "^7.16.7" + "@babel/traverse" "^7.16.7" + "@babel/types" "^7.16.7" "@babel/helper-simple-access@^7.1.0", "@babel/helper-simple-access@^7.16.7": "integrity" "sha512-ZIzHVyoeLMvXMN/vok/a4LWRy8G2v205mNP0XOuf9XRLyX5/u9CnVulUtDgUTama3lT+bf/UqucuZjqiGuTS1g==" @@ -295,6 +308,14 @@ "@babel/helper-remap-async-to-generator" "^7.1.0" "@babel/plugin-syntax-async-generators" "^7.2.0" +"@babel/plugin-proposal-class-properties@^7.16.7": + "integrity" "sha512-IobU0Xme31ewjYOShSIqd/ZGM/r/cuOz2z0MDbNrhF5FW+ZVgi0f2lyeoj9KFPDOAqsYxmLWZte1WOwlvY9aww==" + "resolved" "https://registry.npmjs.org/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.16.7.tgz" + "version" "7.16.7" + dependencies: + "@babel/helper-create-class-features-plugin" "^7.16.7" + "@babel/helper-plugin-utils" "^7.16.7" + "@babel/plugin-proposal-dynamic-import@^7.5.0": "integrity" "sha512-x/iMjggsKTFHYC6g11PL7Qy58IK8H5zqfm9e6hu4z1iH2IRyAp9u9dL80zA6R76yFovETFLKz2VJIC2iIPBuFw==" "resolved" "https://registry.npmjs.org/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.5.0.tgz" @@ -758,7 +779,7 @@ "@babel/parser" "^7.16.7" "@babel/types" "^7.16.7" -"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.16.7", "@babel/traverse@^7.17.0", "@babel/traverse@^7.17.3", "@babel/traverse@^7.4.3", "@babel/traverse@^7.4.4", "@babel/traverse@^7.5.5": +"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.16.7", "@babel/traverse@^7.17.0", "@babel/traverse@^7.17.3", "@babel/traverse@^7.4.3", "@babel/traverse@^7.4.4": "integrity" "sha512-5irClVky7TxRWIRtxlh2WPUUOLhcPN06AGgaQSB8AEwuyEBgJVuJ5imdHm5zxk8w0QS5T+tDfnDxAlhWjpb7cw==" "resolved" "https://registry.npmjs.org/@babel/traverse/-/traverse-7.17.3.tgz" "version" "7.17.3"