wip: added cache

This commit is contained in:
sauravdhakal12
2026-02-27 21:26:36 +05:45
parent 90b0192cd2
commit 024702dd26
9 changed files with 252 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import {
ForbiddenException,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
@@ -8,10 +9,10 @@ import {
UpdateOrganizationRequestDTO,
} from './dtos';
import { PrismaService } from 'src/prisma/prisma.service';
import { RequestContextService } from 'core/als/request-context.service';
import { ORG_ROLE } from 'prisma/generated/prisma/enums';
import { AuthorizationService } from 'src/authorization/authorization.service';
import { USER_ORGANIZATION_OPERATIONS } from 'src/authorization/operations';
import { CacheService } from 'src/cache/cache.service';
@Injectable()
export class OrganizationService {
@@ -19,6 +20,7 @@ export class OrganizationService {
private readonly prisma: PrismaService,
// private readonly reqContext: RequestContextService,
private readonly authorization: AuthorizationService,
private readonly cacheService: CacheService,
) {}
async createNewOrganization(
userId: string,
@@ -58,7 +60,7 @@ export class OrganizationService {
orgId: string,
dto: UpdateOrganizationRequestDTO,
) {
const orgExists = await this.findById(orgId);
const orgExists = await this.organizationExists(orgId);
if (!orgExists) throw new NotFoundException('Organization');
const canUserUpdateOrganization =
@@ -81,7 +83,7 @@ export class OrganizationService {
// TODO: Either empty or choose someone to be owner
async deleteAnOrganization(userId: string, orgId: string) {
const orgExists = await this.findById(orgId);
const orgExists = await this.organizationExists(orgId);
if (!orgExists) throw new NotFoundException('Organization');
const canUserDeleteOrganization =
@@ -110,11 +112,28 @@ export class OrganizationService {
},
});
await this.cacheService.deleteKey(orgId);
return deletedOrganization;
});
}
async organizationExists(orgId: string) {
return await this.prisma.organization.findUnique({
where: { id: orgId },
select: { id: true },
});
}
async findById(orgId: string) {
return await this.prisma.organization.findUnique({ where: { id: orgId } });
const organization = await this.cacheService.getOrSet(
orgId,
async () =>
await this.prisma.organization.findUnique({
where: { id: orgId },
}),
);
return organization;
}
}