Uecko_ERP/apps/server/src/contexts/auth/domain/entities/tab-context.ts

82 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-02-04 14:58:33 +00:00
import { DomainEntity, Result, UniqueID } from "@common/domain";
export interface ITabContextProps {
tabId: UniqueID;
userId: UniqueID;
companyId?: UniqueID;
branchId?: UniqueID;
}
export interface ITabContext {
tabId: UniqueID;
userId: UniqueID;
companyId?: UniqueID;
branchId?: UniqueID;
assignCompany(companyId: UniqueID): void;
assignBranch(branchId: UniqueID): void;
toPersistenceData(): any;
}
export class TabContext extends DomainEntity<ITabContextProps> implements ITabContext {
private _companyId: UniqueID | undefined;
private _branchId: UniqueID | undefined;
static create(props: ITabContextProps, id?: UniqueID): Result<TabContext, Error> {
return Result.ok(new this(props, id));
}
protected constructor(props: ITabContextProps, id?: UniqueID) {
const { tabId, userId, companyId, branchId } = props;
super({ tabId, userId }, id);
this._companyId = companyId;
this._branchId = branchId;
}
get tabId(): UniqueID {
return this._props.tabId;
}
get userId(): UniqueID {
return this._props.userId;
}
get companyId(): UniqueID | undefined {
return this._companyId;
}
get branchId(): UniqueID | undefined {
return this._branchId;
}
hasCompanyAssigned(): boolean {
return this._companyId !== undefined;
}
assignCompany(companyId: UniqueID): void {
this._companyId = companyId;
}
hasBranchAssigned(): boolean {
return this._branchId !== undefined;
}
assignBranch(branchId: UniqueID): void {
this._branchId = branchId;
}
/**
* 🔹 Devuelve una representación lista para persistencia
*/
toPersistenceData(): any {
return {
id: this._id.toString(),
user_id: this.userId.toString(),
company_id: this._companyId ? this._companyId.toString() : undefined,
branchId: this._branchId ? this._branchId.toString() : undefined,
};
}
}