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

Can not remove belongsTo relation #244

Open
frans-beech-it opened this issue Sep 20, 2019 · 1 comment
Open

Can not remove belongsTo relation #244

frans-beech-it opened this issue Sep 20, 2019 · 1 comment

Comments

@frans-beech-it
Copy link
Contributor

Currently the library doesn't support removing an BelongsTo relation. You can set the relation to null but this isn't send to the backend

My current fix:

  /**
     * Fix clearing of belongsTo relation
     *
     * @param data
     */
    protected getRelationships(data: any): any {
        let relationships: any;

        const belongsToMetadata: any[] = Reflect.getMetadata('BelongsTo', data) || [];
        const hasManyMetadata: any[] = Reflect.getMetadata('HasMany', data) || [];

        for (const key in data) {
            if (data.hasOwnProperty(key)) {
                if (data[key] instanceof JsonApiModel) {
                    relationships = relationships || {};

                    if (data[key].id) {
                        const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key);
                        const relationshipKey = entity.relationship;
                        relationships[relationshipKey] = {
                            data: this.buildSingleRelationshipData(data[key])
                        };
                    }
                } else if (data[key] instanceof Array) {
                    const entity = hasManyMetadata.find((entity: any) => entity.propertyName === key);
                    if (entity && this.isValidToManyRelation(data[key])) {
                        relationships = relationships || {};

                        const relationshipKey = entity.relationship;
                        const relationshipData = data[key]
                            .filter((model: JsonApiModel) => model.id)
                            .map((model: JsonApiModel) => this.buildSingleRelationshipData(model));

                        relationships[relationshipKey] = {
                            data: relationshipData
                        };
                    }
                } else if (data[key] === null) {
                    const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key);

                    if (entity) {
                        relationships = relationships || {};

                        relationships[entity.relationship] = {
                            data: null
                        };
                    }
                }
            }
        }

        return relationships;
    }

The updateRelationships method can not handle relationshipKey: {data: null} response.
This is fixed with:

    /**
     * @param model
     * @param relationships
     */
    protected updateRelationships<T extends JsonApiModel>(model: T, relationships: any): T {
        const modelsTypes: any = Reflect.getMetadata('JsonApiDatastoreConfig', this.constructor).models;

        for (const relationship in relationships) {
            if (relationships.hasOwnProperty(relationship) && model.hasOwnProperty(relationship) && model[relationship]) {
                const relationshipModel: JsonApiModel = model[relationship];
                const hasMany: any[] = Reflect.getMetadata('HasMany', relationshipModel);
                const propertyHasMany: any = find(hasMany, (property) => {
                    return modelsTypes[property.relationship] === model.constructor;
                });

                if (propertyHasMany) {
                    relationshipModel[propertyHasMany.propertyName] = relationshipModel[propertyHasMany.propertyName] || [];

                    const indexOfModel = relationshipModel[propertyHasMany.propertyName].indexOf(model);

                    if (indexOfModel === -1) {
                        relationshipModel[propertyHasMany.propertyName].push(model);
                    } else {
                        relationshipModel[propertyHasMany.propertyName][indexOfModel] = model;
                    }
                }
            }
        }

        return model;
    }
frans-beech-it added a commit to frans-beech-it/angular2-jsonapi that referenced this issue Sep 20, 2019
@frans-beech-it
Copy link
Contributor Author

Related PR #245

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