Skip to content

Commit

Permalink
fix(feedback): Serialize the enum as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
smith-opennms committed Aug 24, 2018
1 parent 500632a commit 3c2f997
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/dao/SituationFeedbackDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class SituationFeedbackDAO extends BaseDAO {
* @param {OnmsSituationFeedback[]} feedback - The [[OnmsSituationFeedback]].
*/
public async saveFeedback(feedback: OnmsSituationFeedback[], situationId: number): Promise<void> {
return this.post(this.pathToEndpoint() + '/' + situationId, feedback);
return this.post(this.pathToEndpoint() + '/' + situationId, this.serializeFeedback(feedback));
}

/**
Expand Down Expand Up @@ -93,6 +93,24 @@ export class SituationFeedbackDAO extends BaseDAO {
return feedback;
}

/**
* Serialize the feedbackType as a string.
* @hidden
*/
public serializeFeedback(feedback: OnmsSituationFeedback[]): any[] {
const serializeFeedback = [];
feedback.forEach((fb) => {
// Create a shallow clone
const sfb = Object.assign({}, fb) as any;
// Set the type to the id, to avoid serializing it as an object
if (sfb.feedbackType !== null) {
sfb.feedbackType = sfb.feedbackType.id;
}
serializeFeedback.push(sfb);
});
return serializeFeedback;
}

/**
* Call a POST request in the format the SituationFeedback API expects.
* @hidden
Expand Down
14 changes: 13 additions & 1 deletion test/dao/SituationFeedbackDAO.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { SituationFeedbackDAO } from '../../src/dao/SituationFeedbackDAO';
import { OnmsSituationFeedback } from '../../src/model/OnmsSituationFeedback';

import { MockHTTP23 } from '../rest/MockHTTP23';
import { OnmsSituationFeedbackType } from '../../src/model/OnmsSituationFeedbackType';
import { OnmsSituationFeedbackType, FeedbackTypes } from '../../src/model/OnmsSituationFeedbackType';

const SERVER_NAME = 'Demo';
const SERVER_URL = 'http://demo.opennms.org/opennms/';
Expand Down Expand Up @@ -53,4 +53,16 @@ describe('SituationfeedbackDAO with v1 API', () => {
expect(feedback[0].timestamp).toEqual(1533835399918);
});
});
it('SituationFeedbackDAO.serializeFeedback()', () => {
const feedback = new OnmsSituationFeedback();
feedback.alarmKey = 'some-key';
feedback.fingerprint = 'hash#';
feedback.feedbackType = FeedbackTypes.CORRECT;
const serializeFeedback = dao.serializeFeedback([feedback]);
expect(serializeFeedback[0].feedbackType).toEqual('CORRECT');
// Original entry should be unchanged
expect(feedback.feedbackType).toEqual(FeedbackTypes.CORRECT);
expect(JSON.stringify(serializeFeedback)).toEqual(
'[{"alarmKey":"some-key","fingerprint":"hash#","feedbackType":"CORRECT"}]');
});
});

0 comments on commit 3c2f997

Please sign in to comment.