text: const express = require("express");
  const crypto = require("crypto");
  const app = express();
  
  app.use(express.json());
  
  const customers = [];
  
  function verifyIfExistAccountCpf(request, response, next) {
    const { cpf } = request.headers;
  
    const customer = customers.find((customer) => customer.cpf === cpf);
  
    if (!customer) {
      return response.status(404).json({ message: "Customer not found" });
    }
  
    request.customer = customer;
  
    return next();
  }
  
  function getBalance(statement) {
    const initialValue = 0;
  
    const balance = statement.reduce((acc, operation) => {
      if (operation.type === "credit") return acc + operation;
      if (operation.type === "debit") return acc - operation;
  
      return balance;
    }, initialValue);
  }
  
  app.post("/account", (req, res) => {
    const { cpf, name } = req.body;
    const id = crypto.randomUUID();
  
    const customerAlreadyExists = customers.some(
      (customer) => customer.cpf === cpf
    );
  
    if (customerAlreadyExists) {
      return res.status(400).json({ message: "Customer already exists" });
    }
  
    customers.push({ cpf, name, id, statement: [] });
  
    res.status(201).json(customers);
  });
  
  app.get("/statement/", verifyIfExistAccountCpf, (request, response) => {
    const { customer } = request;
    const data = { statement: customer.statement, cpf: customer.cpf };
  
    return response.status(200).json(data);
  });
  
  app.post("/deposit", verifyIfExistAccountCpf, (req, res) => {
    const { amount, description } = req.body;
    console.log(amount, description);
    const { customer } = req;
  
    const depositOperation = {
      amount,
      description,
      createdAt: new Date(),
      type: "deposit",
    };
  
    customer.statement.push(depositOperation);
  
    return res.status(201).json(depositOperation);
  });
  
  app.post("withdraw", verifyIfExistAccountCpf, (request, response) => {
    const { amount } = request.body;
    const { customer } = request;
  
    const balance = getBalance(customer.statement);
  
    if (balance < amount) {
      return response.status(400).json({ error: "Insuficient funds" });
    }
  
    const statementOperation = {
      amount,
      created_at: new Date(),
      type: "debit",
    };
  
    customer.statement.push(statementOperation);
  
    return response.status(201).send();
  });
  
  app.get("/statement/date", verifyIfExistAccountCpf, (request, response) => {
    const { date } = request.query;
    const { customer } = request;
  
    const dateFormat = new Date(date + "00:00");
  
    const statement = customer.statement.filter(
      (statement) =>
        statement.created_at.toDateString() ===
        new Date(dateFormat).toDateString()
    );
  
    return response.json(statement);
  });
  
  app.get("/", (req, res) => {
    res.json({ ok: "ok" });
  });
  
  app.listen(3333, () => {
    console.log("Server is running");
  });
  