Keys auf SSE umgestellt

This commit is contained in:
Bastian Wagner
2026-03-05 14:51:45 +01:00
parent f88fe93182
commit ac2117b64b
13 changed files with 217 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ import { FindOperator, IsNull, Not } from 'typeorm';
import { faker } from '@faker-js/faker';
import { ConfigService } from '@nestjs/config';
import { MailService } from '../mail/mail.service';
import { SseService } from '../realtime/sse/sse.service';
@Injectable()
export class KeyService {
@@ -23,7 +24,10 @@ export class KeyService {
private readonly helper: HelperService,
private readonly configService: ConfigService,
private readonly mailService: MailService,
) {}
private readonly sseService: SseService
) {
console.log("INIT KEYSERVICE")
}
get isDevelopMode(): boolean {
return (this.configService.get('DEVELOP_MODE') || '').toLowerCase() == 'true';
@@ -87,7 +91,19 @@ export class KeyService {
console.error(e);
}
}
return this.keyrepository.save(this.keyrepository.create(key));
const saved = await this.keyrepository.save(this.keyrepository.create(key));
this.sendKeysToSSE(saved);
return saved;
}
private async sendKeysToSSE(key: Key) {
const system = await this.helper.getSystemOfKey(key)
for (let manager of system.managers) {
const keys = await this.getUsersKeys(manager);
this.sseService.sendKeysToUsers(manager.id, keys)
}
}
@@ -129,7 +145,6 @@ export class KeyService {
where: { id: keyID },
relations: [ 'cylinder', 'cylinder.system', 'cylinder.system.managers', 'cylinder.system.managers.settings' ]
});
console.log(managerOb.cylinder[0].system.managers)
managerOb.cylinder[0].system.managers.filter(m => m.settings.sendSystemUpdateMails).forEach(m => {
this.mailService.sendKeyHandoutMail({ to: m, key, handoutAction: res })
})
@@ -137,7 +152,7 @@ export class KeyService {
} catch (e){
console.log(e)
}
this.sendKeysToSSE(key);
return res;
}
@@ -173,18 +188,22 @@ export class KeyService {
}
}
async createKey(user: User, key: any) {
const k = await this.keyrepository.save(this.keyrepository.create(key));
async createKey(user: User, key: any): Promise<Key> {
const k = await this.keyrepository.save(this.keyrepository.create(key)) as any as Key;
this.activityService.logKeyCreated(user, key, key.cylinder[0].system);
this.sendKeysToSSE(k as any)
return k;
}
async deleteKey(user: User, id: string) {
async deleteKey(user: User, id: string): Promise<Key> {
const key = await this.keyrepository.findOneOrFail({
where: { id, cylinder: { system: { managers: { id: user.id } } } },
});
await this.activityService.logDeleteKey(user, key);
return this.keyrepository.softRemove(key);
const k = await this.keyrepository.softRemove(key);
this.sendKeysToSSE(k)
return k;
}
getDeletedKeys(user: User) {
@@ -198,7 +217,7 @@ export class KeyService {
});
}
async restoreKey(user: User, keyID: string) {
async restoreKey(user: User, keyID: string): Promise<Key> {
const key = await this.keyrepository.findOneOrFail({
where: { cylinder: { system: { managers: { id: user.id } } }, id: keyID },
@@ -207,6 +226,8 @@ export class KeyService {
key.deletedAt = null;
await this.activityService.logKeyRestored(user, key);
await this.helper.deleteKeyArchiveCache();
return this.keyrepository.save(key);
const k = await this.keyrepository.save(key);
this.sendKeysToSSE(k)
return k;
}
}