├── .gitignore ├── src ├── Tax.ts ├── Installment.ts └── Transaction.ts ├── package.json ├── test ├── Installment.test.ts └── Transaction.test.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | coverage -------------------------------------------------------------------------------- /src/Tax.ts: -------------------------------------------------------------------------------- 1 | export default class Tax { 2 | 3 | constructor (readonly paymentMethod: string, readonly percentage: number, readonly amount: number) { 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tdd_fullcycle", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tsc && jest ./dist", 8 | "coverage": "tsc && jest ./dist --coverage" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "@types/jest": "^27.0.3", 15 | "date-fns": "^2.27.0", 16 | "jest": "^27.4.4", 17 | "typescript": "^4.5.3" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Installment.ts: -------------------------------------------------------------------------------- 1 | import Tax from "./Tax"; 2 | 3 | export default class Installment { 4 | status: string; 5 | mdr = 0; 6 | 7 | constructor (readonly number: number, readonly amount: number, readonly tax: Tax) { 8 | this.status = "waiting_payment"; 9 | this.calculateMdr(); 10 | } 11 | 12 | calculateMdr () { 13 | if (this.tax.amount) { 14 | this.mdr += this.tax.amount; 15 | } 16 | if (this.tax.percentage) { 17 | this.mdr += ((this.tax.percentage * this.amount)/100); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/Installment.test.ts: -------------------------------------------------------------------------------- 1 | import Installment from "../src/Installment"; 2 | import Tax from "../src/Tax"; 3 | 4 | test("Deve criar uma parcela com MDR percentual e absoluto", function () { 5 | const tax = new Tax("credit_card", 1, 0.5); 6 | const installment = new Installment(1, 1000, tax); 7 | expect(installment.mdr).toBe(10.50); 8 | }); 9 | 10 | test("Deve criar uma parcela sem MDR", function () { 11 | const tax = new Tax("credit_card", 0, 0); 12 | const installment = new Installment(1, 1000, tax); 13 | expect(installment.mdr).toBe(0); 14 | }); 15 | 16 | test("Deve criar uma parcela sem MDR percentual", function () { 17 | const tax = new Tax("credit_card", 1, 0); 18 | const installment = new Installment(1, 1000, tax); 19 | expect(installment.mdr).toBe(10); 20 | }); -------------------------------------------------------------------------------- /src/Transaction.ts: -------------------------------------------------------------------------------- 1 | import Installment from "./Installment"; 2 | import Tax from "./Tax"; 3 | 4 | export default class Transaction { 5 | installments: Installment[]; 6 | 7 | constructor (readonly email: string, readonly amount: number, readonly paymentMethod: string, readonly numberOfInstallments: number = 1, readonly tax: Tax) { 8 | this.installments = []; 9 | this.generateInstallments(); 10 | } 11 | 12 | generateInstallments () { 13 | let installmentNumber = 1; 14 | const installmentAmount = this.amount / this.numberOfInstallments; 15 | while (installmentNumber <= this.numberOfInstallments) { 16 | const installment = new Installment(installmentNumber++, installmentAmount, this.tax); 17 | this.installments.push(installment); 18 | } 19 | } 20 | 21 | pay (installmentNumber: number) { 22 | const installment = this.installments.find(installment => installment.number === installmentNumber); 23 | if (!installment) throw new Error(); 24 | installment.status = "paid"; 25 | } 26 | 27 | getBalance () { 28 | let balance = this.amount; 29 | for (const installment of this.installments) { 30 | if (installment.status === "paid") balance -= installment.amount; 31 | } 32 | return balance; 33 | } 34 | 35 | getStatus () { 36 | const balance = this.getBalance(); 37 | if (balance === 0) return "paid"; 38 | return "waiting_payment"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/Transaction.test.ts: -------------------------------------------------------------------------------- 1 | import Tax from "../src/Tax"; 2 | import Transaction from "../src/Transaction"; 3 | 4 | test("Deve criar uma transação aguardando pagamento", function () { 5 | const email = "rodrigo@branas.io"; 6 | const amount = 1000; 7 | const paymentMethod = "boleto"; 8 | const installments = 1; 9 | const tax = new Tax("boleto", 0, 5); 10 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 11 | const status = transaction.getStatus(); 12 | expect(status).toBe("waiting_payment"); 13 | }); 14 | 15 | test("Deve criar uma transação no boleto à vista e fazer o pagamento", function () { 16 | const email = "rodrigo@branas.io"; 17 | const amount = 1000; 18 | const paymentMethod = "boleto"; 19 | const installments = 1; 20 | const tax = new Tax("boleto", 0, 5); 21 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 22 | transaction.pay(1); 23 | const status = transaction.getStatus(); 24 | expect(status).toBe("paid"); 25 | }); 26 | 27 | test("Deve criar uma transação no cartão de crédito em 4 parcelas", function () { 28 | const email = "rodrigo@branas.io"; 29 | const amount = 1000; 30 | const paymentMethod = "credit_card"; 31 | const installments = 4; 32 | const tax = new Tax("credit_card", 1, 0); 33 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 34 | const [installment1, installment2, installment3, installment4] = transaction.installments; 35 | expect(installment1.amount).toBe(250); 36 | expect(installment1.status).toBe("waiting_payment"); 37 | expect(installment2.amount).toBe(250); 38 | expect(installment2.status).toBe("waiting_payment"); 39 | expect(installment3.amount).toBe(250); 40 | expect(installment3.status).toBe("waiting_payment"); 41 | expect(installment4.amount).toBe(250); 42 | expect(installment4.status).toBe("waiting_payment"); 43 | }); 44 | 45 | test("Deve criar uma transação no cartão de crédito em 4 parcelas e pagar a primeira", function () { 46 | const email = "rodrigo@branas.io"; 47 | const amount = 1000; 48 | const paymentMethod = "credit_card"; 49 | const installments = 4; 50 | const tax = new Tax("credit_card", 1, 0); 51 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 52 | transaction.pay(1); 53 | const [installment1, installment2, installment3, installment4] = transaction.installments; 54 | expect(installment1.amount).toBe(250); 55 | expect(installment1.status).toBe("paid"); 56 | expect(installment2.amount).toBe(250); 57 | expect(installment2.status).toBe("waiting_payment"); 58 | expect(installment3.amount).toBe(250); 59 | expect(installment3.status).toBe("waiting_payment"); 60 | expect(installment4.amount).toBe(250); 61 | expect(installment4.status).toBe("waiting_payment"); 62 | const balance = transaction.getBalance(); 63 | expect(balance).toBe(750); 64 | }); 65 | 66 | test("Deve criar uma transação no boleto à vista, fazer o pagamento e calcular o MDR (Merchant Discount Rate)", function () { 67 | const email = "rodrigo@branas.io"; 68 | const amount = 1000; 69 | const paymentMethod = "boleto"; 70 | const installments = 1; 71 | const tax = new Tax("boleto", 0, 5); 72 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 73 | transaction.pay(1); 74 | const [installment1] = transaction.installments; 75 | expect(installment1.mdr).toBe(5); 76 | }); 77 | 78 | test("Deve criar uma transação no cartão de crédito em 4 parcelas, fazer o pagamento e calcular o MDR (Merchant Discount Rate)", function () { 79 | const email = "rodrigo@branas.io"; 80 | const amount = 1000; 81 | const paymentMethod = "credit_card"; 82 | const installments = 4; 83 | const tax = new Tax("credit_card", 1, 0); 84 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 85 | transaction.pay(1); 86 | const [installment1] = transaction.installments; 87 | expect(installment1.mdr).toBe(2.5); 88 | }); 89 | 90 | test("Deve criar uma transação no cartão de crédito em 4 parcelas, fazer o pagamento e calcular o MDR (Merchant Discount Rate) variável e fixo", function () { 91 | const email = "rodrigo@branas.io"; 92 | const amount = 1000; 93 | const paymentMethod = "credit_card"; 94 | const installments = 4; 95 | const tax = new Tax("credit_card", 1, 0.50); 96 | const transaction = new Transaction(email, amount, paymentMethod, installments, tax); 97 | transaction.pay(1); 98 | const [installment1] = transaction.installments; 99 | expect(installment1.mdr).toBe(3); 100 | }); 101 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 4 | 5 | /* Projects */ 6 | "incremental": true, /* Enable incremental compilation */ 7 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 8 | // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */ 9 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ 10 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 11 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 12 | 13 | /* Language and Environment */ 14 | "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 15 | // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 16 | // "jsx": "preserve", /* Specify what JSX code is generated. */ 17 | // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ 18 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 19 | // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ 20 | // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 21 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ 22 | // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ 23 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 24 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 25 | 26 | /* Modules */ 27 | "module": "commonjs", /* Specify what module code is generated. */ 28 | // "rootDir": "./", /* Specify the root folder within your source files. */ 29 | // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ 30 | // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ 31 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 32 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 33 | // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ 34 | // "types": [], /* Specify type package names to be included without being referenced in a source file. */ 35 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 36 | // "resolveJsonModule": true, /* Enable importing .json files */ 37 | // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ 38 | 39 | /* JavaScript Support */ 40 | // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ 41 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 42 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ 43 | 44 | /* Emit */ 45 | // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 46 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 47 | // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 48 | // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ 49 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ 50 | "outDir": "./dist", /* Specify an output folder for all emitted files. */ 51 | // "removeComments": true, /* Disable emitting comments. */ 52 | // "noEmit": true, /* Disable emitting files from a compilation. */ 53 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 54 | // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ 55 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 56 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 57 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 58 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 59 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 60 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 61 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 62 | // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ 63 | // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ 64 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 65 | // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ 66 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 67 | // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ 68 | 69 | /* Interop Constraints */ 70 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 71 | // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 72 | "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */ 73 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 74 | "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ 75 | 76 | /* Type Checking */ 77 | "strict": true, /* Enable all strict type-checking options. */ 78 | // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ 79 | // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ 80 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 81 | // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ 82 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 83 | // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ 84 | // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ 85 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 86 | // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ 87 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ 88 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 89 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 90 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 91 | // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ 92 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 93 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ 94 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 95 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 96 | 97 | /* Completeness */ 98 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 99 | "skipLibCheck": true /* Skip type checking all .d.ts files. */ 100 | }, 101 | "include": [ 102 | "src", 103 | "test" 104 | ] 105 | } 106 | --------------------------------------------------------------------------------