Part 4. Clock-in/out System: Basic backend (III) - AppModule

This post is part of a Series of post which I'm describing a clock-in/out system if you want to read more you can read the following posts:


In the previous post, I presented the module UserModule which is used to manage the information about the user. So, I recommend you that you read the first post before this because you can understand the whole system.

This post will presented the AppModule which is used to manage the request of the user from an Angular frontend. This module only have a service, controller and module. The AppService inject the two services: UserService and AuthService from the modules UsersModule and AuthModule. This service is used as a wrapper which use both services.

So, the first step is show the AppModule code:

app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AuthModule } from 'modules/auth/auth.module';
import { UsersModule } from 'modules/users/users.module';
import { AppService } from 'app.service';
@Module({
  imports: [AuthModule, UsersModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

You can see that this module imports AuthModule and UsersModule which is very important because the AppService injected AuthService and UsersService.

So, the next step is develop the controller which contains two routes associated to clock-in and clock-out which invoke the AppService (which will invoke AuthService) and one route associate to get the list of users which must be working now and the timestamp from the server (to get near-realtime which will invoke UserService).

app.controller.ts
import { Get, Controller, Post, Body } from '@nestjs/common';
import { AuthDto } from 'modules/auth/dto/auth.dto';
import { AuthResponseDto } from 'modules/auth/dto/auth.response.dto';
import { AppService } from 'app.service';
import { User } from 'modules/users/entities/user.entity';

@Controller()
export class AppController {
  constructor(private appService: AppService) {}

  @Post('in')
  async authIn(@Body() ticket: AuthDto): Promise< AuthResponseDto > {
    return await this.appService.authIn(ticket);
  }

  @Post('out')
  async authOut(@Body() ticket: AuthDto): Promise< AuthResponseDto > {
    return await this.appService.authOut(ticket);
  }
  @Get('users')
  async usersTicketing(): Promise< { users: User[]; timestamp: number } > {
    return await this.appService.usersTicketing();
  }
}

So, the final step is develop the AppService which uses the two previously mentioned services.

app.service.ts
import { Injectable } from '@nestjs/common';
import { AuthService } from 'modules/auth/auth.service';
import { UserService } from 'modules/users/services/users.service';
import { AuthDto } from 'modules/auth/dto/auth.dto';
import * as moment from 'moment';
import { User } from 'modules/users/entities/user.entity';

@Injectable()
export class AppService {
  constructor(
    private readonly authService: AuthService,
    private readonly usersService: UserService,
  ) {}

  public async authIn(ticket: AuthDto) {
    return await this.authService.authIn(ticket);
  }
  public async authOut(ticket: AuthDto) {
    return await this.authService.authOut(ticket);
  }
  public async usersTicketing(): Promise< { users: User[]; timestamp: number } > {
    const usersMustBeWorking = await this.usersService.getUsersMustBeWorkingNow();
    return {
      users: usersMustBeWorking,
      timestamp: moment().unix(),
    };
  }
}

Resume

‌In this post I've explain my AppModule which is very simple because I'm using clean code in my coding. This module is used to communicate client-side (Angular) and server-side. This module import AuthModule and UsersModule which exports their services. These services are used to save the information about the clock-in/out and get all users which must be working.

At the moment, we've developed a basic backend with three modules and different endpoints. In the following post about backend, we will explain about the seed of the database and testing. However, in the following post of this series we will explain how to build a basic frontend using Angular and RxJS.