Skip to content

Commit 50a18a5

Browse files
authored
Merge pull request #364 from zohreh-deriv/financial_assessment_new_API
feat: Add Financial assessment Questions API
2 parents f0f03f2 + d4cc077 commit 50a18a5

16 files changed

+874
-627
lines changed

bom-core

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
// ignore_for_file: prefer_single_quotes, unnecessary_import, unused_import
2+
3+
import 'package:equatable/equatable.dart';
4+
5+
import 'package:flutter_deriv_api/helpers/helpers.dart';
6+
7+
/// Financial assessment questions response model class.
8+
abstract class FinancialAssessmentQuestionsResponseModel {
9+
/// Initializes Financial assessment questions response model class .
10+
const FinancialAssessmentQuestionsResponseModel({
11+
this.financialAssessmentQuestions,
12+
});
13+
14+
/// The financial assessment questionnaire structure
15+
final FinancialAssessmentQuestions? financialAssessmentQuestions;
16+
}
17+
18+
/// Financial assessment questions response class.
19+
class FinancialAssessmentQuestionsResponse
20+
extends FinancialAssessmentQuestionsResponseModel {
21+
/// Initializes Financial assessment questions response class.
22+
const FinancialAssessmentQuestionsResponse({
23+
super.financialAssessmentQuestions,
24+
});
25+
26+
/// Creates an instance from JSON.
27+
factory FinancialAssessmentQuestionsResponse.fromJson(
28+
dynamic financialAssessmentQuestionsJson,
29+
) =>
30+
FinancialAssessmentQuestionsResponse(
31+
financialAssessmentQuestions: financialAssessmentQuestionsJson == null
32+
? null
33+
: FinancialAssessmentQuestions.fromJson(
34+
financialAssessmentQuestionsJson),
35+
);
36+
37+
/// Converts an instance to JSON.
38+
Map<String, dynamic> toJson() {
39+
final Map<String, dynamic> resultMap = <String, dynamic>{};
40+
41+
if (financialAssessmentQuestions != null) {
42+
resultMap['financial_assessment_questions'] =
43+
financialAssessmentQuestions!.toJson();
44+
}
45+
46+
return resultMap;
47+
}
48+
49+
/// Creates a copy of instance with given parameters.
50+
FinancialAssessmentQuestionsResponse copyWith({
51+
FinancialAssessmentQuestions? financialAssessmentQuestions,
52+
}) =>
53+
FinancialAssessmentQuestionsResponse(
54+
financialAssessmentQuestions:
55+
financialAssessmentQuestions ?? this.financialAssessmentQuestions,
56+
);
57+
}
58+
59+
/// TypeEnum mapper.
60+
final Map<String, TypeEnum> typeEnumMapper = <String, TypeEnum>{
61+
"free_text": TypeEnum.freeText,
62+
"single_choice": TypeEnum.singleChoice,
63+
"multiple_choice": TypeEnum.multipleChoice,
64+
};
65+
66+
/// Type Enum.
67+
enum TypeEnum {
68+
/// free_text.
69+
freeText,
70+
71+
/// single_choice.
72+
singleChoice,
73+
74+
/// multiple_choice.
75+
multipleChoice,
76+
}
77+
78+
/// Financial assessment questions model class.
79+
abstract class FinancialAssessmentQuestionsModel {
80+
/// Initializes Financial assessment questions model class .
81+
const FinancialAssessmentQuestionsModel({
82+
this.questions,
83+
this.version,
84+
});
85+
86+
/// Object containing the questions and possible answers
87+
final Map<String, QuestionsProperty>? questions;
88+
89+
/// The version of the financial assessment questionnaire.
90+
final String? version;
91+
}
92+
93+
/// Financial assessment questions class.
94+
class FinancialAssessmentQuestions extends FinancialAssessmentQuestionsModel
95+
with EquatableMixin {
96+
/// Initializes Financial assessment questions class.
97+
const FinancialAssessmentQuestions({
98+
super.questions,
99+
super.version,
100+
});
101+
102+
/// Creates an instance from JSON.
103+
factory FinancialAssessmentQuestions.fromJson(Map<String, dynamic> json) =>
104+
FinancialAssessmentQuestions(
105+
questions: json['questions'] == null
106+
? null
107+
: Map<String, QuestionsProperty>.fromEntries(json['questions']
108+
.entries
109+
.map<MapEntry<String, QuestionsProperty>>(
110+
(MapEntry<String, dynamic> entry) =>
111+
MapEntry<String, QuestionsProperty>(entry.key,
112+
QuestionsProperty.fromJson(entry.value)))),
113+
version: json['version'],
114+
);
115+
116+
/// Converts an instance to JSON.
117+
Map<String, dynamic> toJson() {
118+
final Map<String, dynamic> resultMap = <String, dynamic>{};
119+
120+
resultMap['questions'] = questions;
121+
resultMap['version'] = version;
122+
123+
return resultMap;
124+
}
125+
126+
/// Creates a copy of instance with given parameters.
127+
FinancialAssessmentQuestions copyWith({
128+
Map<String, QuestionsProperty>? questions,
129+
String? version,
130+
}) =>
131+
FinancialAssessmentQuestions(
132+
questions: questions ?? this.questions,
133+
version: version ?? this.version,
134+
);
135+
136+
@override
137+
List<Object?> get props => [questions, version];
138+
}
139+
140+
/// Questions property model class.
141+
abstract class QuestionsPropertyModel {
142+
/// Initializes Questions property model class .
143+
const QuestionsPropertyModel({
144+
required this.type,
145+
required this.question,
146+
required this.hideIf,
147+
required this.answers,
148+
});
149+
150+
/// The type of input required for this question
151+
final TypeEnum type;
152+
153+
/// The question text to display
154+
final String question;
155+
156+
/// Conditions that determine if this question should be hidden
157+
final List<String> hideIf;
158+
159+
/// Array of possible answers for this question
160+
final List<AnswersItem> answers;
161+
}
162+
163+
/// Questions property class.
164+
class QuestionsProperty extends QuestionsPropertyModel with EquatableMixin {
165+
/// Initializes Questions property class.
166+
const QuestionsProperty({
167+
required super.answers,
168+
required super.hideIf,
169+
required super.question,
170+
required super.type,
171+
});
172+
173+
/// Creates an instance from JSON.
174+
factory QuestionsProperty.fromJson(Map<String, dynamic> json) =>
175+
QuestionsProperty(
176+
answers: List<AnswersItem>.from(
177+
json['answers'].map(
178+
(dynamic item) => AnswersItem.fromJson(item),
179+
),
180+
),
181+
hideIf: List<String>.from(
182+
json['hide_if'].map(
183+
(dynamic item) => item,
184+
),
185+
),
186+
question: json['question'],
187+
type: typeEnumMapper[json['type']]!,
188+
);
189+
190+
/// Converts an instance to JSON.
191+
Map<String, dynamic> toJson() {
192+
final Map<String, dynamic> resultMap = <String, dynamic>{};
193+
194+
resultMap['answers'] = answers
195+
.map<dynamic>(
196+
(AnswersItem item) => item.toJson(),
197+
)
198+
.toList();
199+
200+
resultMap['hide_if'] = hideIf
201+
.map<dynamic>(
202+
(String item) => item,
203+
)
204+
.toList();
205+
206+
resultMap['question'] = question;
207+
resultMap['type'] = typeEnumMapper.entries
208+
.firstWhere((MapEntry<String, TypeEnum> entry) => entry.value == type)
209+
.key;
210+
211+
return resultMap;
212+
}
213+
214+
/// Creates a copy of instance with given parameters.
215+
QuestionsProperty copyWith({
216+
List<AnswersItem>? answers,
217+
List<String>? hideIf,
218+
String? question,
219+
TypeEnum? type,
220+
}) =>
221+
QuestionsProperty(
222+
answers: answers ?? this.answers,
223+
hideIf: hideIf ?? this.hideIf,
224+
question: question ?? this.question,
225+
type: type ?? this.type,
226+
);
227+
228+
@override
229+
List<Object?> get props => [answers, hideIf, question, type];
230+
}
231+
232+
/// Answers item model class.
233+
abstract class AnswersItemModel {
234+
/// Initializes Answers item model class .
235+
const AnswersItemModel({
236+
required this.value,
237+
required this.key,
238+
required this.hideIf,
239+
this.nextNode,
240+
});
241+
242+
/// Display text for this answer option
243+
final String value;
244+
245+
/// The key for the answer option
246+
final String key;
247+
248+
/// Array of conditions that determine if this answer should be hidden
249+
final List<String> hideIf;
250+
251+
/// The next question to show after this answer is selected
252+
final String? nextNode;
253+
}
254+
255+
/// Answers item class.
256+
class AnswersItem extends AnswersItemModel with EquatableMixin {
257+
/// Initializes Answers item class.
258+
const AnswersItem({
259+
required super.hideIf,
260+
required super.key,
261+
required super.value,
262+
super.nextNode,
263+
});
264+
265+
/// Creates an instance from JSON.
266+
factory AnswersItem.fromJson(Map<String, dynamic> json) => AnswersItem(
267+
hideIf: List<String>.from(
268+
json['hide_if'].map(
269+
(dynamic item) => item,
270+
),
271+
),
272+
key: json['key'],
273+
value: json['value'],
274+
nextNode: json['next_node'],
275+
);
276+
277+
/// Converts an instance to JSON.
278+
Map<String, dynamic> toJson() {
279+
final Map<String, dynamic> resultMap = <String, dynamic>{};
280+
281+
resultMap['hide_if'] = hideIf
282+
.map<dynamic>(
283+
(String item) => item,
284+
)
285+
.toList();
286+
287+
resultMap['key'] = key;
288+
resultMap['value'] = value;
289+
resultMap['next_node'] = nextNode;
290+
291+
return resultMap;
292+
}
293+
294+
/// Creates a copy of instance with given parameters.
295+
AnswersItem copyWith({
296+
List<String>? hideIf,
297+
String? key,
298+
String? value,
299+
String? nextNode,
300+
}) =>
301+
AnswersItem(
302+
hideIf: hideIf ?? this.hideIf,
303+
key: key ?? this.key,
304+
value: value ?? this.value,
305+
nextNode: nextNode ?? this.nextNode,
306+
);
307+
308+
@override
309+
List<Object?> get props => [hideIf, key, value, nextNode];
310+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:deriv_dependency_injector/dependency_injector.dart';
2+
import 'package:flutter_deriv_api/api/exceptions/base_api_exception.dart';
3+
import 'package:flutter_deriv_api/api/models/base_exception_model.dart';
4+
import 'package:flutter_deriv_api/api/response/financial_assessment_questions_response_result.dart';
5+
import 'package:flutter_deriv_api/basic_api/generated/financial_assessment_questions_receive.dart';
6+
import 'package:flutter_deriv_api/basic_api/generated/financial_assessment_questions_send.dart';
7+
import 'package:flutter_deriv_api/helpers/helpers.dart';
8+
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
9+
10+
/// Extended functionality for [FinancialAssessmentQuestionsResponse] class to implement API call methods.
11+
class FinancialAssessmentQuestionsResponseExtended
12+
extends FinancialAssessmentQuestionsResponse {
13+
static final BaseAPI _api = Injector()<BaseAPI>();
14+
15+
/// Fetch Financial Assessment Questions.
16+
Future<FinancialAssessmentQuestionsResponse>
17+
fetchFinancialAssessmentQuestions({
18+
required FinancialAssessmentQuestionsRequest request,
19+
}) async {
20+
final FinancialAssessmentQuestionsReceive response =
21+
await _api.call(request: request);
22+
23+
checkException(
24+
response: response,
25+
exceptionCreator: ({BaseExceptionModel? baseExceptionModel}) =>
26+
BaseAPIException(baseExceptionModel: baseExceptionModel),
27+
);
28+
29+
return FinancialAssessmentQuestionsResponse.fromJson(
30+
response.financialAssessmentQuestions,
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)