Skip to content

Commit c452810

Browse files
authored
Merge pull request #277 from SCVApp/master
order by position
2 parents 124a49c + 2bd0649 commit c452810

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/lockers/entities/locker.entity.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ export class LockerEntity {
1717

1818
@Column({ type: 'varchar', length: 5 })
1919
identifier: string;
20+
21+
@Column({ type: 'int', default: 0 })
22+
position: number;
2023
}

src/lockers/lockers.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Module } from '@nestjs/common';
22
import { TypeOrmModule } from '@nestjs/typeorm';
33
import { CommonModule } from 'src/common/common.module';
4+
import { NotificationModule } from 'src/notification/notification.module';
45
import { PassModule } from 'src/pass/pass.module';
56
import { LockerEntity } from './entities/locker.entity';
67
import { LockerControllerEntity } from './entities/lockerController.entity';
@@ -18,6 +19,7 @@ import { LockersService } from './lockers.service';
1819
]),
1920
CommonModule,
2021
PassModule,
22+
NotificationModule,
2123
],
2224
controllers: [LockersController],
2325
providers: [LockersService, LockersGateway],

src/lockers/lockers.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { PassService } from 'src/pass/service/pass.service';
1717
import { UserPassEntity } from 'src/pass/entities/passUser.entity';
1818
import { ControllerWithActiveLockerCount } from './types/controllerWithActiveLockerCount.type';
1919
import { LockerWithActiveUser } from './types/lockerWithActiveUser.type';
20+
import { NotificationService } from 'src/notification/notification.service';
2021

2122
@Injectable()
2223
export class LockersService {
@@ -33,6 +34,8 @@ export class LockersService {
3334
private readonly jwtService: JwtService,
3435
@Inject(forwardRef(() => PassService))
3536
private readonly passService: PassService,
37+
@Inject(forwardRef(() => NotificationService))
38+
private readonly notificationService: NotificationService,
3639
) {}
3740

3841
async getControllersWithAvailableLockers(): Promise<
@@ -223,10 +226,11 @@ export class LockersService {
223226
locker.controller.token,
224227
locker.identifier,
225228
);
226-
return await this.lockersGateway.openLocker(
229+
const success = await this.lockersGateway.openLocker(
227230
locker.controller.id.toString(),
228231
jwtToken,
229232
);
233+
return success;
230234
}
231235

232236
async openLockerById(lockerId: number): Promise<void> {
@@ -284,6 +288,7 @@ export class LockersService {
284288
.addSelect('lockers_users.start_time', 'startTime')
285289
.addSelect('lockers_users.end_time', 'endTime')
286290
.addSelect('user_passes.azure_id', 'azureId')
291+
.orderBy('lockers.position', 'ASC')
287292
.getRawMany();
288293

289294
return lockersData.map((data) => {

src/notification/notification.service.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { DeviceEntity } from './entities/device.entity';
1212
import { PassService } from 'src/pass/service/pass.service';
1313
import { ApiKeyEntity } from './entities/apiKey.entity';
1414
import * as crypto from 'crypto';
15+
import { UserPassEntity } from 'src/pass/entities/passUser.entity';
1516

1617
@Injectable()
1718
export class NotificationService {
@@ -179,4 +180,29 @@ export class NotificationService {
179180
async findApiKey(key: string) {
180181
return this.apiKeyRepository.findOne({ where: { key } });
181182
}
183+
184+
async sendToUser(user: UserPassEntity, title: string, body: string) {
185+
const devices = await this.deviceRepository.find({ where: { user } });
186+
const tokens = devices.map((device) => device.notification_token);
187+
for (const token of tokens) {
188+
this.FCM.send(
189+
{
190+
notification: {
191+
title,
192+
body,
193+
},
194+
token,
195+
},
196+
(err, response) => {
197+
if (err) {
198+
this.logger.error('Notification sending failed');
199+
this.logger.error(err);
200+
} else {
201+
this.logger.log('Notification sent');
202+
this.logger.log(response);
203+
}
204+
},
205+
);
206+
}
207+
}
182208
}

0 commit comments

Comments
 (0)