109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
import { Result, UniqueID } from "@common/domain";
|
|
import { ITransactionManager } from "@common/infrastructure/database";
|
|
import { TabContext } from "../domain";
|
|
import { ITabContextRepository } from "../domain/repositories/tab-context-repository.interface";
|
|
import { ITabContextService } from "./tab-context-service.interface";
|
|
|
|
export class TabContextService implements ITabContextService {
|
|
private readonly _respository!: ITabContextRepository;
|
|
private readonly _transactionManager!: ITransactionManager;
|
|
|
|
constructor(repository: ITabContextRepository, transactionManager: ITransactionManager) {
|
|
this._respository = repository;
|
|
this._transactionManager = transactionManager;
|
|
}
|
|
|
|
/**
|
|
* Obtiene el contexto de una pestaña por su ID
|
|
*/
|
|
async getByTabId(tabId: UniqueID): Promise<Result<TabContext, Error>> {
|
|
try {
|
|
return await this._transactionManager.complete(async (transaction) => {
|
|
// Verificar si la pestaña existe
|
|
const tabContextOrError = await this._respository.getContextByTabId(tabId, transaction);
|
|
if (tabContextOrError.isSuccess && !tabContextOrError.data) {
|
|
return Result.fail(new Error("Invalid or expired Tab ID"));
|
|
}
|
|
|
|
if (tabContextOrError.isFailure) {
|
|
return Result.fail(tabContextOrError.error);
|
|
}
|
|
|
|
return Result.ok(tabContextOrError.data);
|
|
});
|
|
} catch (error: unknown) {
|
|
return Result.fail(error as Error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Registra un nuevo contexto de pestaña para un usuario
|
|
*/
|
|
async createContext(tabId: UniqueID, userId: UniqueID): Promise<Result<TabContext, Error>> {
|
|
if (!userId || !tabId) {
|
|
return Result.fail(new Error("User ID and Tab ID are required"));
|
|
}
|
|
|
|
try {
|
|
return await this._transactionManager.complete(async (transaction) => {
|
|
const contextOrError = TabContext.create(
|
|
{
|
|
userId,
|
|
tabId,
|
|
},
|
|
UniqueID.generateNewID().data
|
|
);
|
|
|
|
if (contextOrError.isFailure) {
|
|
return Result.fail(contextOrError.error);
|
|
}
|
|
|
|
await this._respository.createContext(contextOrError.data, transaction);
|
|
|
|
return Result.ok(contextOrError.data);
|
|
});
|
|
} catch (error: unknown) {
|
|
return Result.fail(error as Error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Asigna una empresa activa a un contexto de pestaña
|
|
*/
|
|
async assignCompany(tabId: UniqueID, companyId: UniqueID): Promise<Result<void, Error>> {
|
|
if (!companyId || !tabId) {
|
|
return Result.fail(new Error("Tab ID and Company ID are required"));
|
|
}
|
|
|
|
try {
|
|
return await this._transactionManager.complete(async (transaction) => {
|
|
// Verificar si la pestaña existe
|
|
const tabContextOrError = await this._respository.contextExists(tabId, transaction);
|
|
if (tabContextOrError.isFailure || !tabContextOrError.data) {
|
|
return Result.fail(new Error("Invalid or expired Tab ID"));
|
|
}
|
|
|
|
return await this._respository.updateCompanyByTabId(tabId, companyId, transaction);
|
|
});
|
|
} catch (error: unknown) {
|
|
return Result.fail(error as Error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Elimina un contexto de pestaña por su ID
|
|
*/
|
|
async removeContext(tabId: UniqueID): Promise<Result<void, Error>> {
|
|
if (!tabId) {
|
|
return Result.fail(new Error("Tab ID is required"));
|
|
}
|
|
try {
|
|
return await this._transactionManager.complete(async (transaction) => {
|
|
return await this._respository.deleteContextByTabId(tabId, transaction);
|
|
});
|
|
} catch (error: unknown) {
|
|
return Result.fail(error as Error);
|
|
}
|
|
}
|
|
}
|