Skip to content

Commit aa566ce

Browse files
authored
Merge pull request #285 from SCVApp/master
Optimze user fatching
2 parents 990ca1b + ea8d2e2 commit aa566ce

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

src/lockers/lockers.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export class LockersService {
159159
userAccessToken,
160160
);
161161
if (!user) {
162+
this.logger.error('User not found');
162163
throw new NotFoundException('User not found');
163164
}
164165

src/notification/notification.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ export class NotificationService {
9999
azure_id: string,
100100
device_id: string,
101101
accessToken: string,
102-
) {
102+
): Promise<void> {
103103
const user = await this.passService.getUserFromAzureId(
104104
azure_id,
105105
accessToken,
106106
);
107107

108108
if (!user) {
109-
throw new NotFoundException('User not found');
109+
return;
110110
}
111111

112112
await this.passService.getUserAccessLevel(user, accessToken);
@@ -134,7 +134,7 @@ export class NotificationService {
134134
try {
135135
await this.deviceRepository.save(newDevice);
136136
} catch (e) {
137-
this.logger.error(e);
137+
this.logger.error("Couldn't save device", e);
138138
}
139139
}
140140

src/pass/service/pass.service.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,30 @@ export class PassService {
4747
private readonly passGateway: PassGateway,
4848
) {}
4949
private openDoor: string[] = [];
50-
async getUserFromAzureId(azureId: string = '', accessToken: string) {
50+
async getUserFromAzureId(azureId: string, accessToken: string) {
5151
if (accessToken === '') {
5252
throw new UnauthorizedException("accessToken can't be empty");
5353
}
54-
let userFromAzure = undefined;
55-
if (azureId === '') {
56-
userFromAzure = await this.userService.getMe(accessToken);
57-
if (!userFromAzure || !userFromAzure.id) {
54+
if (!azureId || azureId === '') {
55+
const userFromAzure = await this.userService.getMe(accessToken);
56+
if (!userFromAzure || !userFromAzure.id || userFromAzure.id === '') {
5857
return null;
5958
}
59+
azureId = userFromAzure.id;
6060
}
6161
const user = await this.userPassRepository.findOne({
62-
where: { azure_id: azureId === '' ? userFromAzure.id : azureId },
62+
where: { azure_id: azureId },
6363
});
6464
if (user) {
6565
return user;
6666
}
6767
try {
68-
if (azureId !== '') {
69-
userFromAzure = await this.searchService.searchSpecificUser(
70-
accessToken,
71-
azureId,
72-
);
73-
}
74-
if (!userFromAzure && !userFromAzure.id) {
75-
return null;
76-
}
77-
return this.userPassRepository.save({
78-
azure_id: userFromAzure.id,
68+
return await this.userPassRepository.save({
69+
azure_id: azureId,
7970
});
8071
} catch (e) {
8172
return await this.userPassRepository.findOne({
82-
where: { azure_id: azureId === '' ? userFromAzure.id : azureId },
73+
where: { azure_id: azureId },
8374
});
8475
}
8576
}

0 commit comments

Comments
 (0)