xiongzhu 2 tahun lalu
induk
melakukan
791a89be6a
2 mengubah file dengan 20 tambahan dan 4 penghapusan
  1. 8 1
      src/users/users.controller.ts
  2. 12 3
      src/users/users.service.ts

+ 8 - 1
src/users/users.controller.ts

@@ -14,6 +14,7 @@ import { UserProfileDto } from './dto/user-profile.dto'
 import { IUsers } from './interfaces/users.interface'
 import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
 import { Users } from './entities/users.entity'
+import { Public } from 'src/auth/public.decorator'
 
 @ApiTags('users')
 @Controller('users')
@@ -74,4 +75,10 @@ export class UsersController {
             status: HttpStatus.OK
         }
     }
-}
+
+    @Public()
+    @Get('/:userId/hasInvite')
+    public async hasInvite(@Param('userId') userId: string){
+        return await this.usersService.hasInvite(parseInt(userId))
+    }
+}

+ 12 - 3
src/users/users.service.ts

@@ -8,7 +8,7 @@ import {
     InternalServerErrorException,
     UnauthorizedException
 } from '@nestjs/common'
-import { Repository, UpdateResult } from 'typeorm'
+import { And, LessThan, LessThanOrEqual, MoreThan, MoreThanOrEqual, Repository, UpdateResult } from 'typeorm'
 import { InjectRepository } from '@nestjs/typeorm'
 import { Users } from './entities/users.entity'
 import { IUsers } from './interfaces/users.interface'
@@ -21,6 +21,7 @@ import * as randomstring from 'randomstring'
 import { paginate, Pagination } from 'nestjs-typeorm-paginate'
 import { Role } from 'src/model/role.enum'
 import { PageRequest } from '../common/dto/page-request'
+import { endOfDay, startOfDay } from 'date-fns'
 
 @Injectable()
 export class UsersService {
@@ -100,11 +101,11 @@ export class UsersService {
     public async login(username: string, password: string): Promise<Users> {
         let user = await this.userRepository.findOneBy({ username })
         if (!user) {
-            throw new UnauthorizedException('Username and password doesn\'t match')
+            throw new UnauthorizedException("Username and password doesn't match")
         }
         const isMatch = await this.hashingService.compare(password, user.password)
         if (!isMatch) {
-            throw new UnauthorizedException('Username and password doesn\'t match')
+            throw new UnauthorizedException("Username and password doesn't match")
         }
         return user
     }
@@ -197,4 +198,12 @@ export class UsersService {
         user.iat = iat || Math.floor(new Date().getTime() / 1000) - 1
         return await this.userRepository.save(user)
     }
+
+    public async hasInvite(userId: number) {
+        const user = await this.userRepository.findOneBy({
+            invitor: userId,
+            createdAt: And(MoreThanOrEqual(startOfDay(new Date())), LessThanOrEqual(endOfDay(new Date())))
+        })
+        return !!user
+    }
 }