forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoft-delete.service.ts
More file actions
49 lines (46 loc) · 1.66 KB
/
Copy pathsoft-delete.service.ts
File metadata and controls
49 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Split } from '../entities/split.entity';
import { Participant } from '../entities/participant.entity';
/**
* Service for soft-delete and restore of Split and Participant.
* Use repository.softDelete(id) or repository.softRemove(entity) for DELETE;
* use this service's restore methods for admin restore.
*/
@Injectable()
export class SoftDeleteService {
constructor(
@InjectRepository(Split)
private readonly splitRepository: Repository<Split>,
@InjectRepository(Participant)
private readonly participantRepository: Repository<Participant>,
) {}
async restoreSplit(id: string): Promise<Split> {
const result = await this.splitRepository.restore(id);
if (result.affected === 0) {
throw new NotFoundException(`Split with ID ${id} not found or not soft-deleted`);
}
const split = await this.splitRepository.findOne({ where: { id } });
if (!split) {
throw new NotFoundException(`Split with ID ${id} not found`);
}
return split;
}
async restoreParticipant(id: string): Promise<Participant> {
const result = await this.participantRepository.restore(id);
if (result.affected === 0) {
throw new NotFoundException(
`Participant with ID ${id} not found or not soft-deleted`,
);
}
const participant = await this.participantRepository.findOne({
where: { id },
relations: ['split'],
});
if (!participant) {
throw new NotFoundException(`Participant with ID ${id} not found`);
}
return participant;
}
}