Skip to content

Subject is undefined when working with soft-deleted entities. #20

Open
@collinadilecca

Description

@collinadilecca

Hey, I'm using your package for one of my projects and it saved me a ton of time. But I am having a little issue.
I'm using a polymorphic entity called Activity. It has the parent entities User, Property, Contact, CalendarEvent, Role.
Just for clarification, it is used for tracking user activities on the mentioned entities in the system. It works like a charm when the entities are created or updated, but when the entity is deleted, the subject is undefined. That is pretty reasonable since I wasn't using soft-delete. I switched to it and it is still behaving the same. I'm using Nestjs with Postgres.

@EntityRepository(Activity)
@Entity('activities')
export class Activity
    extends AbstractPolymorphicRepository<Activity>
    implements PolymorphicChildInterface
{
    @PolymorphicParent(() => [User, Contact, Role, Property, CalendarEvent], {
        eager: true,
    })
    subject: User | Contact | Role | Property | CalendarEvent;

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        nullable: true,
    })
    userId: number;

    @Column()
    entityType: string;

    @Column()
    entityId: number;

    @Column({
        nullable: true,
    })
    message: string;

    @CreateDateColumn()
    timestamp: Date;

    @ManyToOne(() => User, { eager: true })
    @JoinColumn({ name: 'userId', referencedColumnName: 'id' })
    user: User;
}

This is my polymorphic entity

@Entity('roles')
export class Role {
    @ApiModelProperty({ readOnly: true })
    @PrimaryGeneratedColumn()
    id: number;

    @ApiModelProperty()
    @Column({
        unique: true,
    })
    @Validator.IsNotEmpty()
    @Validator.IsString()
    @Validator.MinLength(2, {
        message: 'Name should be at least three characters long',
    })
    name: string;

    @ApiModelProperty()
    @Column()
    permissions: string;

    @Exclude()
    @DeleteDateColumn()
    deletedAt: Date;

    @OneToMany(() => User, (user) => user.role)
    @JoinColumn({ name: 'id', referencedColumnName: 'roleId' })
    users: User[];

    @PolymorphicChildren(() => Activity, {
        eager: false,
    })
    activities: Activity[];
}

This is my role entity with polymorphic implementation

@Injectable()
@EntityRepository(Activity)
export class ActivitiesService extends AbstractPolymorphicRepository<Activity> {
    constructor(
        @InjectRepository(Activity)
        private readonly repository: Repository<Activity>,
    ) {
        super();
    }

    async getAllActivities(): Promise<Activity[] | HttpException> {
        const activities = await this.repository.find({
            withDeleted: true,
        });
        if (activities) {
            return activities;
        } else {
            return new HttpException(
                'No activities found',
                HttpStatus.NOT_FOUND,
            );
        }
    }
}

Activities service

{
        "id": 2,
        "userId": 2,
        "entityType": "Role",
        "entityId": 12,
        "message": "Role has been created",
        "timestamp": "2021-08-30T20:38:46.992Z",
        "user": {
            "id": 2,
            "roleId": 1,
            "name": "John",
            "email": "[email protected]",
            "role": {
                "id": 1,
                "name": "Agent",
                "permissions": ""
            }
        },
        "subject": {
            "id": 12,
            "name": "TestRole",
            "permissions": "create,update"
        }
    },
    {
        "id": 3,
        "userId": 2,
        "entityType": "Role",
        "entityId": 2,
        "message": "Role has been deleted",
        "timestamp": "2021-08-30T20:38:50.980Z",
        "user": {
            "id": 2,
            "roleId": 1,
            "name": "John",
            "email": "[email protected]",
            "role": {
                "id": 1,
                "name": "Agent",
                "permissions": ""
            }
        }
    }

Postman testing JSON response where I have previously created a Role entity and then soft-deleted one. The first one has a subject. Second one's subject is undefined.
Is there any way to include them from the polymorphic service or repo? I can include consoled queries if needed. They are pretty long so I didn't add them initially. The same goes for the Role service. Thanks for the help! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions