2023-07-31 02:37:09 -07:00
|
|
|
import { Service } from 'typedi';
|
|
|
|
import { CacheService } from './cache.service';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { SharedWorkflowRepository, UserRepository } from '@/databases/repositories';
|
2023-07-31 02:37:09 -07:00
|
|
|
import type { User } from '@/databases/entities/User';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { RoleService } from './role.service';
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
@Service()
|
|
|
|
export class OwnershipService {
|
|
|
|
constructor(
|
|
|
|
private cacheService: CacheService,
|
|
|
|
private userRepository: UserRepository,
|
2023-08-02 23:58:36 -07:00
|
|
|
private roleService: RoleService,
|
2023-07-31 02:37:09 -07:00
|
|
|
private sharedWorkflowRepository: SharedWorkflowRepository,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the user who owns the workflow. Note that workflow ownership is **immutable**.
|
|
|
|
*/
|
|
|
|
async getWorkflowOwnerCached(workflowId: string) {
|
2023-08-02 03:51:25 -07:00
|
|
|
const cachedValue = (await this.cacheService.get(`cache:workflow-owner:${workflowId}`)) as User;
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
if (cachedValue) return this.userRepository.create(cachedValue);
|
|
|
|
|
2023-08-02 23:58:36 -07:00
|
|
|
const workflowOwnerRole = await this.roleService.findWorkflowOwnerRole();
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
if (!workflowOwnerRole) throw new Error('Failed to find workflow owner role');
|
|
|
|
|
|
|
|
const sharedWorkflow = await this.sharedWorkflowRepository.findOneOrFail({
|
|
|
|
where: { workflowId, roleId: workflowOwnerRole.id },
|
|
|
|
relations: ['user', 'user.globalRole'],
|
|
|
|
});
|
|
|
|
|
|
|
|
void this.cacheService.set(`cache:workflow-owner:${workflowId}`, sharedWorkflow.user);
|
|
|
|
|
|
|
|
return sharedWorkflow.user;
|
|
|
|
}
|
|
|
|
}
|