Skip to content

mausham-shrestha-aligent/aligent_challenge

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Aligent Date Time Programming Challenge

This is the programming challenge from Aligent Consulting for Backend Developer position.

Description

Create an API that can be used to:

  1. Find out the number of days between two datetime parameters.
  2. Find out the number of weekdays between two datetime parameters.
  3. Find out the number of complete weeks between two datetime parameters.
  4. Accept a third parameter to convert the result of (1, 2 or 3) into one of seconds, minutes, hours, years.
  5. Allow the specification of a timezone for comparison of input parameters from different timezones.

This Project uses AdonisJS as a framework

AdonisJS is a backend framework for Node.js. The framework is written in TypeScript and the application created using AdonisJS is also in TypeScript. It includes everything that is required to create a fully functional web app or an API server. AdonisJS offers a stable ecosystem to write server-side web application. Further explanation can be found here

Luxon

The luxon library is used for dealing with date time in this project.

Testing the API

Japa

Japa is a test runner to create test runners. It is a tiny Node.js test runner that can be used to test apps or even create test runner. It is simple, fast and has minimal core. It doesn't ship with any CLI. Read More

SuperTest

In this project, supertest is used to make HTTP requests. More about the supertest can be found here.

Command for running the test

npm run test

OR

node -r @adonisjs/assembler/build/register japaFile.ts test

Middleware

In this project middleware is used to check the validity of the input. Middleware is a series of function that are executed during an HTTP request before it reaches the route handler. Every middleware class must implement the handle method to handle the http request and call the next method to forward the request to the next middleware or the route handler. More about the middleware in adonis can be found here.

Link for the Further explanation

Running the code

Notes:

  • AdonisJS is a Node.js framework, and hence it requires Node.js to be installed on computer. We need at least the latest release of Node.js v14.
  • Use command below to install all the required packages
    npm install
    
  • API development environment : Postman

Command for running the app

node ace serve --watch

OR

npm run dev

The port number is defined in .env file which is 80. So, after running the app, the user needs to enter http://localhost/ in the browser for GET request and http://localhost/ROUTENAME in the API development Environment for POST request.

WHERE ROUTENAME can be listed using the command below:

Command for listing all the routes

node ace list:routes
Method Route Handler Middleware Name
HEAD, GET /uploads/* Closure drive.local.serve
HEAD, GET / Closure
POST /days DaysController.handleRequest
POST /weeks WeeksController.handleRequest
POST /weekdays WeekDays.controller.handleRequest

API Description

Note:

These api accepts "first_date" and "second_date" as two parameters and optional parameters which are "format(Output type)" and "timezone" to give the required results.


http://localhost/days

This API is for calculation of numbers of days between two dates

Example 1

Input with first_date and second_date parameter

{ 
  "first_date" : "2019-03-20T00:01:00 Asia/Damascus" ,
  "second_date" : "2019-03-17T00:01:00 Asia/kuwait"
}

Result in default format

{ 
  "result" : 3 
}
Example 2

Input with format

{ 
  "first_date" : "2022-09-20T00:01:00 Asia/Damascus" ,
  "second_date" : "2021-03-21T00:01:00 Asia/kuwait",
  "format" : "seconds"  
}

Output in Seconds

{ 
  "result" : 47347200
}
Example 3

Input with format and Timezone

{ 
  "first_date" : "2022-03-20T00:01:00 Asia/Damascus" ,
  "second_date" : "2021-03-21T00:01:00 Asia/kuwait",
  "format" : "seconds" ,
  "timezone" : "Asia/Kuala_lumpur"
}

Output in seconds

{ 
  "result" : 31449600
}
Data Validation Example

Input with wrong first_date parameter

{
  "first_date": "sdfdsfsdf",
  "second_date": "2021-03-21T00:01:00 Asia/Kuwait",
  "format": "days",
  "timezone": "Asia/Kuala_Lumpur"
}

Result

{
  "error": "Date is in incorrect form",
  "parameter": "first_date"
}

http://localhost/weeks

This API is for calculation of numbers of complete weeks between two dates

Example 1

Input with all the possible parameters

{ 
  "first_date" : "2017-03-20T00:01:00 Asia/Damascus" ,
  "second_date" : "2014-03-17T12:01:00 Asia/kuwait",
  "format" : "minutes" ,
  "timezone" : "Asia/Kuwait"
}

Output in Minutes

{ "result" : 1572480  }
Example 2

Input all the parameters except timezone.

{
  "first_date": "2021-05-01T00:00:00",
  "second_date": "2021-05-15T00:00:00",
  "format": "seconds"
}
      

Result in seconds

{
    "result": 1209600
}
Example 3

Input with all the parameters and expected result in minutes

{
  "first_date": "2021-03-20T00:01:00",
  "second_date": "2021-05-23T00:01:00",
  "format": "minutes",
  "timezone" : "Asia/kuwait"
}

Result in minutes

{
    "result": 90720
}
Data Validation example for /weeks API

Input with incorret format parameter

{
  "first_date": "2021-03-20T00:01:00 Asia/Damascus",
  "second_date": "2021-03-21T00:01:00 Asia/Kuwait",
  "format": "secon",
  "timezone": "Asia/Kuala_Lumpur"
}
      

Expected Output

{
    "error": "Parameter is not acceptable",
    "parameter": "format"
}

http://localhost/weekdays

This API is for calculation of numbers of weekdays between two dates
Example 1

Input with first_date and second_date parameter

{ 
  "first_date" : "2022-03-20T00:01:00 Asia/Damascus" ,
  "second_date" : "2021-03-17T00:01:00 Asia/kuwait"
}

Output in default format

{ 
  "result" : 263 
}
Example 2

Input with all the parameters except timezone

{
  "first_date": "2021-05-01T00:00:00",
  "second_date": "2021-05-15T00:00:00",
  "format": "seconds"
}

Output in seconds

{
    "result": 864000
}
Example 3

Input with all the parameters and expected number of weekdays in years.

{
  "first_date": "2021-03-20T00:01:00 Australia/sydney",
  "second_date": "2023-05-17T00:01:00",
  "format": "years",
  "timezone" : "Asia/kuwait"
}

Output in years

{
    "result": 1
}
Data Validation example for /weekdays

Input with wrong timezone

{
  "first_date": "2021-03-20T00:01:00 Asia/kuwait",
  "second_date": "2021-01-17T12:02:01 Asia/Kuala_Lumpur",
  "format": "seconds",
  "timezone": "Asiala_Lur"
}

Error shown as a result

{
    "error": "The timezone is invalid",
    "parameter": "timezone"
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.7%
  • Shell 0.3%