local-aws/src/iam/iam.service.ts

50 lines
1.3 KiB
TypeScript

import { Injectable } from "@nestjs/common";
import { PrismaService } from "../_prisma/prisma.service";
import { Prisma } from "@prisma/client";
import { IamPolicy } from "./iam-policy.entity";
import { IamRole } from "./iam-role.entity";
import { EntityAlreadyExists } from "../aws-shared-entities/aws-exceptions";
@Injectable()
export class IamService {
constructor(
private readonly prismaService: PrismaService,
) {}
async createRole(data: Prisma.IamRoleCreateInput): Promise<IamRole> {
try {
const record = await this.prismaService.iamRole.create({ data });
return new IamRole(record);
} catch (err) {
throw new EntityAlreadyExists(`RoleName ${data.name} already exists`);
}
}
async findOneRoleByName(accountId: string, name: string): Promise<IamRole | null> {
const record = await this.prismaService.iamRole.findFirst({
where: {
name,
accountId,
}
});
return record ? new IamRole(record) : null;
}
async deleteRoleByName(accountId: string, name: string) {
await this.prismaService.iamRole.deleteMany({
where: {
name,
accountId,
}
});
}
async createPolicy(data: Prisma.IamPolicyCreateInput): Promise<IamPolicy> {
const record = await this.prismaService.iamPolicy.create({ data });
return new IamPolicy(record);
}
}