2023-01-27 20:50:07 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2024-03-20 15:04:03 -05:00
|
|
|
import { DummyValue, GenerateSql } from 'src/decorators';
|
2024-04-19 06:47:29 -04:00
|
|
|
import { SessionEntity } from 'src/entities/session.entity';
|
2024-04-27 16:45:16 -04:00
|
|
|
import { ISessionRepository, SessionSearchOptions } from 'src/interfaces/session.interface';
|
2024-03-20 22:15:09 -05:00
|
|
|
import { Instrumentation } from 'src/utils/instrumentation';
|
2024-04-27 16:45:16 -04:00
|
|
|
import { LessThanOrEqual, Repository } from 'typeorm';
|
2023-01-27 20:50:07 +00:00
|
|
|
|
2024-03-12 01:19:12 -04:00
|
|
|
@Instrumentation()
|
2023-01-27 20:50:07 +00:00
|
|
|
@Injectable()
|
2024-04-19 06:47:29 -04:00
|
|
|
export class SessionRepository implements ISessionRepository {
|
|
|
|
constructor(@InjectRepository(SessionEntity) private repository: Repository<SessionEntity>) {}
|
2023-01-27 20:50:07 +00:00
|
|
|
|
2024-04-27 16:45:16 -04:00
|
|
|
@GenerateSql({ params: [DummyValue.DATE] })
|
|
|
|
search(options: SessionSearchOptions): Promise<SessionEntity[]> {
|
|
|
|
return this.repository.find({ where: { updatedAt: LessThanOrEqual(options.updatedBefore) } });
|
|
|
|
}
|
|
|
|
|
2023-11-30 10:10:30 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.STRING] })
|
2024-04-19 06:47:29 -04:00
|
|
|
getByToken(token: string): Promise<SessionEntity | null> {
|
2024-05-26 18:15:52 -04:00
|
|
|
return this.repository.findOne({
|
|
|
|
where: { token },
|
|
|
|
relations: {
|
|
|
|
user: {
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-01-27 20:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 06:47:29 -04:00
|
|
|
getByUserId(userId: string): Promise<SessionEntity[]> {
|
2023-04-25 22:19:23 -04:00
|
|
|
return this.repository.find({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
relations: {
|
|
|
|
user: true,
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
updatedAt: 'desc',
|
|
|
|
createdAt: 'desc',
|
|
|
|
},
|
|
|
|
});
|
2023-01-27 20:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 23:45:55 -04:00
|
|
|
create<T extends Partial<SessionEntity>>(dto: T): Promise<T & { id: string }> {
|
|
|
|
return this.repository.save(dto);
|
2023-04-25 22:19:23 -04:00
|
|
|
}
|
|
|
|
|
2024-04-20 23:45:55 -04:00
|
|
|
update<T extends Partial<SessionEntity>>(dto: T): Promise<T> {
|
|
|
|
return this.repository.save(dto);
|
2023-04-25 22:19:23 -04:00
|
|
|
}
|
|
|
|
|
2023-11-30 10:10:30 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
2023-10-30 11:48:38 -04:00
|
|
|
async delete(id: string): Promise<void> {
|
|
|
|
await this.repository.delete({ id });
|
2023-01-27 20:50:07 +00:00
|
|
|
}
|
|
|
|
}
|