Skip to content

Commit

Permalink
feat: 알림 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
publdaze committed Aug 23, 2023
1 parent a553ca5 commit 79c54ca
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 5 deletions.
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nestjs/mongoose": "^10.0.1",
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^3.0.3",
"@nestjs/swagger": "^7.1.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
5 changes: 3 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MongooseModule } from '@nestjs/mongoose';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { ScheduleModule } from '@nestjs/schedule';

@Module({
imports: [
Expand All @@ -25,7 +26,7 @@ import { UsersModule } from './users/users.module';
}),
UsersModule,
AuthModule,
ScheduleModule.forRoot(),
],
})
export class AppModule {
}
export class AppModule {}
15 changes: 15 additions & 0 deletions src/auth/auth.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ export class AuthRepository {
console.log('error...');
}
}

async updateExpoToken(user: {
loginId: string;
password: string;
expoToken: string;
}) {
const { loginId, password, expoToken } = user;
return this.usersModel.findOneAndUpdate(
{
loginId,
password,
},
{ expoToken: expoToken },
);
}
}
5 changes: 4 additions & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ export class AuthService {
private readonly authRepository: AuthRepository,
) {}

async login(user: { loginId: string; password: string }) {
async login(user: { loginId: string; password: string; expoToken: string }) {
console.log(user);
const userAccounts = this.authRepository.findByLoginIdAndPassword(user);
return userAccounts
.then((res) => {
if (res.length === 0) {
throw `계정 정보가 없습니다. 입력하신 loginId: ${user.loginId}, password: ${user.password}`;
}

this.authRepository.updateExpoToken(user);

const userByFindFirst = res[0];
const payload = {
username: userByFindFirst.username,
Expand Down
9 changes: 9 additions & 0 deletions src/buildings/buildings.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@ export class BuildingsRepository {
console.error(err);
}
}

async getAll() {
try {
return await this.buildingModel.find({}).exec();
} catch (err) {
console.log('error... get All');
console.error(err);
}
}
}
5 changes: 4 additions & 1 deletion src/buildings/floors/cans/cans.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import {
BuildingSchema,
} from 'src/buildings/entities/building.entity';
import { BuildingsRepository } from 'src/buildings/buildings.repository';
import { User, UserSchema } from '../../../users/entities/user.entity';
import { UsersRepository } from '../../../users/users.repository';

@Module({
imports: [
MongooseModule.forFeature([
{ name: Building.name, schema: BuildingSchema },
{ name: User.name, schema: UserSchema },
]),
],
controllers: [CansController],
providers: [CansService, BuildingsRepository],
providers: [CansService, BuildingsRepository, UsersRepository],
})
export class CansModule {}
39 changes: 38 additions & 1 deletion src/buildings/floors/cans/cans.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Injectable } from '@nestjs/common';
import { BuildingsRepository } from 'src/buildings/buildings.repository';
import { UsersRepository } from '../../../users/users.repository';
import { Cron, CronExpression } from '@nestjs/schedule';

@Injectable()
export class CansService {
constructor(private readonly buildingRepository: BuildingsRepository) {}
constructor(
private readonly buildingRepository: BuildingsRepository,
private readonly userRepository: UsersRepository,
) {}

createCan({
buildingNumber,
Expand Down Expand Up @@ -40,4 +45,36 @@ export class CansService {
console.log(err);
});
}

@Cron(CronExpression.EVERY_5_SECONDS)
handleCron() {
console.log('Called when the current second is 5');
this.userRepository.getByRole('ROLE_CLEANER').then((users) => {
this.buildingRepository.getAll().then((buildings) => {
for (const building of buildings) {
for (const floor of building.floors) {
for (const user of users) {
if (user.expoToken) {
fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: user.expoToken,
title: `이건 테스트입니다`,
body: String(floor.trashCan),
}),
})
.then(() => console.log('send!'))
.catch((err) => console.log(err));
console.log(user);
}
}
}
}
});
});
}
}
3 changes: 3 additions & 0 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export class User {
@Prop()
username: string;

@Prop({ default: null })
expoToken: string;

@Prop()
role: Role;

Expand Down
10 changes: 10 additions & 0 deletions src/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from './entities/user.entity';
import { Role } from './entities/authorities';

@Injectable()
export class UsersRepository {
Expand All @@ -22,4 +23,13 @@ export class UsersRepository {
console.log('error...');
}
}

async getByRole(role: Role): Promise<UserDocument[]> {
try {
return await this.usersModel.find({ role });
} catch (err) {
console.log('Error get users by role:', err.message);
throw err;
}
}
}
1 change: 1 addition & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class UsersService {
user.password = password;
user.username = username;
user.role = role;
user.expoToken = '';

await this.usersRepository.save(user);
user.password = undefined;
Expand Down

0 comments on commit 79c54ca

Please sign in to comment.