Skip to content

Commit c8d5fca

Browse files
committed
fixing relation issues
1 parent 8aced1c commit c8d5fca

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

internal/powerplatform/services/data_record/api_data_record.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ type EntityDefinitionsDto struct {
4747
MetadataID string `json:"MetadataId"`
4848
}
4949

50+
type RelationApiResponse struct {
51+
OdataContext string `json:"@odata.context"`
52+
Value []RelationApiBody `json:"value"`
53+
}
54+
5055
type RelationApiBody struct {
5156
OdataID string `json:"@odata.id"`
5257
}
@@ -286,6 +291,36 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
286291
Path: fmt.Sprintf("/api/data/%s/%s(%s)/%s/$ref", constants.DATAVERSE_API_VERSION, entityDefinition.LogicalCollectionName, result.Id, key),
287292
}
288293

294+
existingRelationsResponse := RelationApiResponse{}
295+
296+
apiResponse, _ := client.Api.Execute(ctx, "GET", apiUrl.String(), nil, nil, []int{http.StatusOK, http.StatusNoContent}, nil)
297+
298+
json.Unmarshal(apiResponse.BodyAsBytes, &existingRelationsResponse)
299+
300+
var toBeDeleted []RelationApiBody = make([]RelationApiBody, 0)
301+
302+
for _, existingRelation := range existingRelationsResponse.Value {
303+
delete := true
304+
for _, nestedItem := range nestedMapList {
305+
nestedMap := nestedItem.(map[string]interface{})
306+
relationEntityDefinition := getEntityDefinition(ctx, client, environmentUrl, nestedMap["table_logical_name"].(string))
307+
if existingRelation.OdataID == fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, relationEntityDefinition.LogicalCollectionName, nestedMap["data_record_id"]) {
308+
delete = false
309+
break
310+
}
311+
}
312+
if delete {
313+
toBeDeleted = append(toBeDeleted, existingRelation)
314+
}
315+
}
316+
317+
for _, relation := range toBeDeleted {
318+
_, err = client.Api.Execute(ctx, "DELETE", relation.OdataID, nil, nil, []int{http.StatusOK, http.StatusNoContent}, nil)
319+
if err != nil {
320+
return nil, err
321+
}
322+
}
323+
289324
for _, nestedItem := range nestedMapList {
290325
nestedMap := nestedItem.(map[string]interface{})
291326

0 commit comments

Comments
 (0)