Last active
October 8, 2025 03:43
-
-
Save sebasmrl/eea04559f45efed7cebb593b6492323b to your computer and use it in GitHub Desktop.
Mini ejercicio practico Cirkular Fintech SAS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log('Mini ejercicio técnico Cirkular Agro Fintech SAS - Sebastian Morales') | |
| //**Instalar paquete con el comando ```npm install uuid``` | |
| import {v5 as uuid} from 'uuid'; | |
| //Definicion de tipos | |
| interface PreInvoice{ | |
| products:string[] | |
| sent:boolean; | |
| } | |
| interface Invoice extends PreInvoice{ | |
| id: string; | |
| } | |
| //Variables globales | |
| export const transactionsQueue:Invoice[] = []; | |
| //Funcion helper para simular lentitud | |
| const sleep = (ms: number) =>{ | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| //Funcion que hace peticion y simula la sincornización | |
| const SyncSimulationRequest = async(transactions: Invoice[], simulateError:boolean = false)=>{ | |
| try{ | |
| //Esperar 2 segundos | |
| await sleep(2000); | |
| if(simulateError) return false; | |
| const rs = await fetch('https://jsonplaceholder.typicode.com/todos/1'); | |
| if (rs.status === 200){ | |
| transactions = transactions.map( transaction => ({ ...transaction, sent:true}) ) | |
| return true; | |
| }else{ | |
| return false; | |
| } | |
| }catch{ | |
| return false; | |
| } | |
| } | |
| // Funcion para hacer una transacción | |
| export const doTransaction = (userId:string, preInvoice: PreInvoice )=>{ | |
| //Generar id unico de la transaccion | |
| const idTransaction = uuid(userId, `${Date.now()}`); | |
| const newTransaction:Invoice = { | |
| ...preInvoice, | |
| id: idTransaction | |
| } | |
| //agregar a la cola de transacciones | |
| transactionsQueue.push(newTransaction); | |
| //simula un error en la petición | |
| return SyncSimulationRequest(transactionsQueue, true); | |
| } | |
| //Evento cuando se tiene conexion a la red | |
| window.addEventListener("online", async(_event) => { | |
| const rs = await SyncSimulationRequest(transactionsQueue); | |
| if(rs) alert('Sincronizacion realizada'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment