Skip to content

order by position #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lockers/entities/locker.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export class LockerEntity {

@Column({ type: 'varchar', length: 5 })
identifier: string;

@Column({ type: 'int', default: 0 })
position: number;
}
2 changes: 2 additions & 0 deletions src/lockers/lockers.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CommonModule } from 'src/common/common.module';
import { NotificationModule } from 'src/notification/notification.module';
import { PassModule } from 'src/pass/pass.module';
import { LockerEntity } from './entities/locker.entity';
import { LockerControllerEntity } from './entities/lockerController.entity';
Expand All @@ -18,6 +19,7 @@ import { LockersService } from './lockers.service';
]),
CommonModule,
PassModule,
NotificationModule,
],
controllers: [LockersController],
providers: [LockersService, LockersGateway],
Expand Down
7 changes: 6 additions & 1 deletion src/lockers/lockers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PassService } from 'src/pass/service/pass.service';
import { UserPassEntity } from 'src/pass/entities/passUser.entity';
import { ControllerWithActiveLockerCount } from './types/controllerWithActiveLockerCount.type';
import { LockerWithActiveUser } from './types/lockerWithActiveUser.type';
import { NotificationService } from 'src/notification/notification.service';

@Injectable()
export class LockersService {
Expand All @@ -33,6 +34,8 @@ export class LockersService {
private readonly jwtService: JwtService,
@Inject(forwardRef(() => PassService))
private readonly passService: PassService,
@Inject(forwardRef(() => NotificationService))
private readonly notificationService: NotificationService,
) {}

async getControllersWithAvailableLockers(): Promise<
Expand Down Expand Up @@ -223,10 +226,11 @@ export class LockersService {
locker.controller.token,
locker.identifier,
);
return await this.lockersGateway.openLocker(
const success = await this.lockersGateway.openLocker(
locker.controller.id.toString(),
jwtToken,
);
return success;
}

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

return lockersData.map((data) => {
Expand Down
26 changes: 26 additions & 0 deletions src/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DeviceEntity } from './entities/device.entity';
import { PassService } from 'src/pass/service/pass.service';
import { ApiKeyEntity } from './entities/apiKey.entity';
import * as crypto from 'crypto';
import { UserPassEntity } from 'src/pass/entities/passUser.entity';

@Injectable()
export class NotificationService {
Expand Down Expand Up @@ -179,4 +180,29 @@ export class NotificationService {
async findApiKey(key: string) {
return this.apiKeyRepository.findOne({ where: { key } });
}

async sendToUser(user: UserPassEntity, title: string, body: string) {
const devices = await this.deviceRepository.find({ where: { user } });
const tokens = devices.map((device) => device.notification_token);
for (const token of tokens) {
this.FCM.send(
{
notification: {
title,
body,
},
token,
},
(err, response) => {
if (err) {
this.logger.error('Notification sending failed');
this.logger.error(err);
} else {
this.logger.log('Notification sent');
this.logger.log(response);
}
},
);
}
}
}
Loading