Created the key.ts controller
This commit is contained in:
parent
e8cf7f4be7
commit
1be7efea8a
35
src/controller/key.ts
Normal file
35
src/controller/key.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { checkProjectAccess } from "@controller/projects";
|
||||
import { AccessKey, IKeyRole } from "@models/AccessKey";
|
||||
import { decryptField, encryptClearField } from "@utils/decryption";
|
||||
import { planLimits } from "../limits/plans";
|
||||
|
||||
export const listKeys = async (userId: string, projectId: string) => {
|
||||
const access = await checkProjectAccess(IKeyRole.VIEW)(userId, projectId);
|
||||
if ("code" in access) return access;
|
||||
|
||||
const keys = await AccessKey.find({ projectId: encryptClearField(String(access._id)) });
|
||||
|
||||
return keys.map(key => ({ id: key._id, name: key.name, role: key.role }));
|
||||
};
|
||||
|
||||
export const createKey = async (userId: string, projectId: string, configuration: { name: string, role: IKeyRole }) => {
|
||||
const access = await checkProjectAccess(IKeyRole.MANAGE)(userId, projectId);
|
||||
if ("code" in access) return access;
|
||||
|
||||
const count = await AccessKey.countDocuments({ projectId: encryptClearField(String(access._id)) });
|
||||
if (count >= planLimits[access.plan].KEYS) return { code: 95, message: "You have exceeded the key limit" };
|
||||
|
||||
const key = await AccessKey.create({ ...configuration, projectId });
|
||||
|
||||
return { token: decryptField(key.token) };
|
||||
};
|
||||
|
||||
export const deleteKey = async (userId: string, projectId: string, keyId: string) => {
|
||||
const access = await checkProjectAccess(IKeyRole.MANAGE)(userId, projectId);
|
||||
if ("code" in access) return access;
|
||||
|
||||
const key = await AccessKey.findOne({ _id: keyId, projectId: encryptClearField(projectId) });
|
||||
if (key === null) return { code: 8002, message: "The provided key could not be found" };
|
||||
|
||||
key.delete();
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user