Skip to content
Permalink
Browse files

fix(feedback): Serialize the enum as a string

  • Loading branch information...
smith-opennms committed Aug 24, 2018
1 parent 500632a commit 3c2f9978f8a1023eaffde0cde6b9f81fb85601ad
Showing with 32 additions and 2 deletions.
  1. +19 −1 src/dao/SituationFeedbackDAO.ts
  2. +13 −1 test/dao/SituationFeedbackDAO.spec.ts
@@ -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));
}

/**
@@ -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
@@ -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/';
@@ -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.
You can’t perform that action at this time.