2024-01-24 04:38:57 -08:00
|
|
|
import { Container } from 'typedi';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
|
|
|
import type { User } from '@db/entities/User';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { CredentialsRepository } from '@db/repositories/credentials.repository';
|
|
|
|
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
|
2024-01-24 04:38:57 -08:00
|
|
|
import type { CredentialSharingRole } from '@db/entities/SharedCredentials';
|
|
|
|
import type { ICredentialsDb } from '@/Interfaces';
|
|
|
|
import type { CredentialPayload } from '../types';
|
2023-11-08 07:29:39 -08:00
|
|
|
|
|
|
|
async function encryptCredentialData(credential: CredentialsEntity) {
|
|
|
|
const { createCredentialsFromCredentialsEntity } = await import('@/CredentialsHelper');
|
|
|
|
const coreCredential = createCredentialsFromCredentialsEntity(credential, true);
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
coreCredential.setData(credential.data);
|
|
|
|
|
|
|
|
return coreCredential.getDataToSave() as ICredentialsDb;
|
|
|
|
}
|
|
|
|
|
2024-01-22 03:29:28 -08:00
|
|
|
const emptyAttributes = {
|
|
|
|
name: 'test',
|
|
|
|
type: 'test',
|
|
|
|
data: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function createManyCredentials(
|
|
|
|
amount: number,
|
|
|
|
attributes: Partial<CredentialsEntity> = emptyAttributes,
|
|
|
|
) {
|
|
|
|
return await Promise.all(
|
|
|
|
Array(amount)
|
|
|
|
.fill(0)
|
|
|
|
.map(async () => await createCredentials(attributes)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createCredentials(attributes: Partial<CredentialsEntity> = emptyAttributes) {
|
|
|
|
const credentialsRepository = Container.get(CredentialsRepository);
|
|
|
|
const entity = credentialsRepository.create(attributes);
|
|
|
|
|
|
|
|
return await credentialsRepository.save(entity);
|
|
|
|
}
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
/**
|
|
|
|
* Save a credential to the test DB, sharing it with a user.
|
|
|
|
*/
|
|
|
|
export async function saveCredential(
|
|
|
|
credentialPayload: CredentialPayload,
|
2024-01-24 04:38:57 -08:00
|
|
|
{ user, role }: { user: User; role: CredentialSharingRole },
|
2023-11-08 07:29:39 -08:00
|
|
|
) {
|
|
|
|
const newCredential = new CredentialsEntity();
|
|
|
|
|
|
|
|
Object.assign(newCredential, credentialPayload);
|
|
|
|
|
|
|
|
const encryptedData = await encryptCredentialData(newCredential);
|
|
|
|
|
|
|
|
Object.assign(newCredential, encryptedData);
|
|
|
|
|
|
|
|
const savedCredential = await Container.get(CredentialsRepository).save(newCredential);
|
|
|
|
|
|
|
|
savedCredential.data = newCredential.data;
|
|
|
|
|
|
|
|
await Container.get(SharedCredentialsRepository).save({
|
|
|
|
user,
|
|
|
|
credentials: savedCredential,
|
|
|
|
role,
|
|
|
|
});
|
|
|
|
|
|
|
|
return savedCredential;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function shareCredentialWithUsers(credential: CredentialsEntity, users: User[]) {
|
|
|
|
const newSharedCredentials = users.map((user) =>
|
|
|
|
Container.get(SharedCredentialsRepository).create({
|
|
|
|
userId: user.id,
|
|
|
|
credentialsId: credential.id,
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'credential:user',
|
2023-11-08 07:29:39 -08:00
|
|
|
}),
|
|
|
|
);
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(SharedCredentialsRepository).save(newSharedCredentials);
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
export function affixRoleToSaveCredential(role: CredentialSharingRole) {
|
2023-11-08 07:29:39 -08:00
|
|
|
return async (credentialPayload: CredentialPayload, { user }: { user: User }) =>
|
2024-01-17 07:08:50 -08:00
|
|
|
await saveCredential(credentialPayload, { user, role });
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllCredentials() {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await Container.get(CredentialsRepository).find();
|
2023-11-08 07:29:39 -08:00
|
|
|
}
|
2023-11-27 02:56:06 -08:00
|
|
|
|
|
|
|
export const getCredentialById = async (id: string) =>
|
2024-01-17 07:08:50 -08:00
|
|
|
await Container.get(CredentialsRepository).findOneBy({ id });
|