Skip to content

wojciechszmelczerczyk/ts-transaction-api

Repository files navigation

Transaction managing API

Description

Simple REST API for transaction handling.

Table of contents

Techstack:

  • Express
  • TypeScript
  • Jest

Requirements:

  • node
  • http client for ex. postman, httpie

Usage

Clone repository

git clone https://github.com/wojciechszmelczerczyk/ts-transaction-api.git

Navigate to project folder

cd /ts-transaction-api

Env setup

Create .env file in root directory.

CSV_FILENAME=transactions.csv

Install dependencies

npm i --force

Run API

npm run dev

Postman collection

You can import provided postman json and play with api.

Architecture

Introduction

Library used to develop app architecture routing-controllers. REST API using Controller, Service and Repository approach.

POST /api/transaction

User send request with date and status body then express rest api intercept request data, validate date with moment library, status with isStatusCorrect function, modify date with modifyDate function and create random uuid. Then with csv-writer library data is save in transactions.csv file. Server respond with modified date.

Example

GET /api/transaction

User send request with page and limit query params then express rest api validate those parameters with validateParams function. With convert-csv-to-json library, csv is being converted to json. By using paginatejson library, pagination is made on json data. In the end paginated json data is being converted back to csv format json2csv and send back to client.

Example

API

Method Endpoint
POST /api/transaction
GET /api/transaction

Modify date helper function

Simple function which takes as an input Date and transaction status Boolean value and returns date in future:

  • when boolean is set to false, returned date should be 5 days in future.
  • when boolean is set to true, returned date should be month in future.

Test function

npm run test:function

ex.

input:

{ "date": "2024-01-31T08:12:59Z", "status": true }

output:

{ "date": "2024-02-29T08:12:59Z" }

Tests

Run tests

npm run test:api

Transaction

POST /api/transaction

when date correct and status true, should return date month in future
test("when date correct and status true, should return date month in future", async () => {
  const res = await request(app)
    .post("/api/transaction")
    .send({ date: "2012-02-02", status: "true" });

  expect(new Date(res.body.modifiedDate)).toStrictEqual(new Date("2012-03-02"));
});
when date correct and status false, should return date 5 days in future
test("when date correct and status false, should return date 5 days in future", async () => {
  const res = await request(app)
    .post("/api/transaction")
    .send({ date: "2012-02-02", status: "false" });

  expect(new Date(res.body.modifiedDate)).toStrictEqual(new Date("2012-02-07"));
});
when date incorrect, should return error message
test("when date incorrect, should return error message", async () => {
  const res = await request(app)
    .post("/api/transaction")
    .send({ date: 2, status: "true" });

  expect(res.body).toStrictEqual({
    err: "Bad date format. String has to be date format",
  });
});
when status incorrect, should return error message
test("when status incorrect, should return error message", async () => {
  const res = await request(app)
    .post("/api/transaction")
    .send({ date: "2012-02-02", status: "x" });

  expect(res.body).toStrictEqual({
    err: "Bad status type. Status has to be either 'true' or 'false'",
  });
});

GET /api/transaction

when page and limit query params correct, should return records specific for params
test("when page and limit query params correct, should return records specific for params", async () => {
  const res = await request(app)
    .get("/api/transaction")
    .query({ page: "1", limit: "2" });

  expect(res).toBeTruthy();
});
when query params exceed number of transactions, should return error message
test("when query params exceed number of transactions, should return error message", async () => {
  const res = await request(app)
    .get("/api/transaction")
    .query({ page: "1000", limit: "2000" });

  expect(res.body.err).toBe("No data available for this parameters");
});
when no query params provided, should return first 5 records
test("when no query params provided, should return first 5 records", async () => {
  const res = await request(app).get("/api/transaction");
  expect(res).toBeTruthy();
});
when one of provided parameters incorrect, should return error message
test("when one of provided parameters incorrect, should return error message", async () => {
  const res = await request(app)
    .get("/api/transaction")
    .query({ page: "x", limit: "2" });

  expect(res.body.err).toBe(
    "Page and limit have to be positive numeric values"
  );
});

Releases

No releases published

Packages

No packages published