Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
collinadilecca opened this issue Aug 30, 2021 · 1 comment
Open

Comments

@collinadilecca
Copy link

collinadilecca commented Aug 30, 2021

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! :)

@collinadilecca
Copy link
Author

@bashleigh, got any advice on this.? It would really save a lot of time for my project, to avoid searching for other solutions since yours is quite neat. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant