Files
backend/libs/common/src/modules/space/entities/space.entity.ts
2024-12-25 10:52:48 +04:00

125 lines
3.1 KiB
TypeScript

import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
Unique,
} from 'typeorm';
import { SpaceDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { UserSpaceEntity } from '../../user/entities';
import { DeviceEntity } from '../../device/entities';
import { CommunityEntity } from '../../community/entities';
import { SubspaceEntity } from './subspace';
import { SpaceLinkEntity } from './space-link.entity';
import { SceneEntity } from '../../scene/entities';
import { SpaceModelEntity } from '../../space-model';
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
import { TagEntity } from './tag.entity';
@Entity({ name: 'space' })
@Unique(['invitationCode'])
export class SpaceEntity extends AbstractEntity<SpaceDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
nullable: false,
})
public uuid: string;
@Column({
nullable: true,
})
public spaceTuyaUuid: string;
@Column({
nullable: false,
})
public spaceName: string;
@ManyToOne(() => CommunityEntity, (community) => community.spaces, {
nullable: false,
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'community_id' })
community: CommunityEntity;
@Column({
nullable: true,
})
public invitationCode: string;
@ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true })
parent: SpaceEntity;
@OneToMany(() => SpaceEntity, (space) => space.parent, {
nullable: false,
onDelete: 'CASCADE',
})
children: SpaceEntity[];
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.space)
userSpaces: UserSpaceEntity[];
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@OneToMany(() => SubspaceEntity, (subspace) => subspace.space, {
nullable: true,
})
subspaces?: SubspaceEntity[];
// Position columns
@Column({ type: 'float', nullable: false, default: 0 })
public x: number; // X coordinate for position
@Column({ type: 'float', nullable: false, default: 0 })
public y: number; // Y coordinate for position
@OneToMany(
() => DeviceEntity,
(devicesSpaceEntity) => devicesSpaceEntity.spaceDevice,
)
devices: DeviceEntity[];
@OneToMany(() => SpaceLinkEntity, (connection) => connection.startSpace, {
nullable: true,
})
public outgoingConnections: SpaceLinkEntity[];
@OneToMany(() => SpaceLinkEntity, (connection) => connection.endSpace, {
nullable: true,
})
public incomingConnections: SpaceLinkEntity[];
@Column({
nullable: true,
type: 'text',
})
public icon: string;
@OneToMany(() => SceneEntity, (scene) => scene.space)
scenes: SceneEntity[];
@ManyToOne(() => SpaceModelEntity, (spaceModel) => spaceModel.spaces, {
nullable: true,
})
spaceModel?: SpaceModelEntity;
@OneToMany(
() => InviteUserSpaceEntity,
(inviteUserSpace) => inviteUserSpace.space,
)
invitedUsers: InviteUserSpaceEntity[];
@OneToMany(() => TagEntity, (tag) => tag.space)
tags: TagEntity[];
constructor(partial: Partial<SpaceEntity>) {
super();
Object.assign(this, partial);
}
}