/coffeeit-assessment

Backend assessment from Coffee IT

Primary LanguageTypeScript

coffee IT logo

๐ŸŽฏ CoffeeIT backend assessment

Backend assessment from CoffeeIT

๐Ÿ“‹ Description

The assessment is to make an API that is able to get the weather information for certain cities.

โฌ‡ Installation

Make sure you have Nodejs and Nestjs/cli installed

~ node -v
~ nest --version
# Clone via SSH or any other method
$ git clone git@github.com:oussamabouchikhi/coffeeit-assessment.git

# CD into the project
$ cd coffeeit-assessment

# Install the dependencies
$ npm install

๐Ÿ› ๏ธ Configuration

Rename the sample.env file to .env and put your Mongodb Atlas connection string & OpenWeatherMap api key

DAYS property is for setting how many days to get fro city weather forcast for the getCityNextXDaysWeather function, by default it will get the next 7 days (we cannot set more than that for the free version)

MONGODB_URI=YOUR_MONGODB_ATLAS_CONNECTION_STRING
OPEN_WEATHER_API_KEY=YOUR_API_KEY

DAYS=NUMBER_OF_DAYS

๐Ÿš€ Running the app

# development
$ npm start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

๐Ÿงช Test

# unit tests (you can add the prefix --watch)
$ yarn test

# e2e tests
$ yarn test:e2e

# test coverage
$ yarn test:cov

Test scheduler

in cities.service.ts file add the bellow code

private count = 0
numberOfSeconds = undefined; // if undefined or unspecified it will default to 7 seconds

  startTime = new Date().getTime();
  @Cron(`0-59/1 * * * * *`, {
    name: 'myJob',
  })
  handleCron() {
    console.log(`${this.count + 1} - Called every second`);
    this.count++;
    this.closeJob();
  }

  closeJob() {
    const job = this.schedulerRegistry.getCronJob('myJob');

    const endTime = this.startTime + 1000 * (this.numberOfSeconds || 7);

    if (job.lastDate().getTime() > endTime) {
      job.stop();
    }
  }

in cities.controller.ts file add the bellow code

@Get('test')
testFunc() {
  return this.citiesService.handleCron();
}

you should get

1 - Called every second
2 - Called every second
3 - Called every second
4 - Called every second
5 - Called every second
6 - Called every second
7 - Called every second

Docker

Make sure docker is running on your machine. And Obviously you installed it :)

# Build docker image
docker-compose build

# Show docker images
docker images

# Show docker images on running container
docker ps -a

# run the image
Docker compose up