File tree Expand file tree Collapse file tree 3 files changed +85
-2
lines changed
Expand file tree Collapse file tree 3 files changed +85
-2
lines changed Original file line number Diff line number Diff line change @@ -11,14 +11,60 @@ module Order = struct
1111 isin : ISINCode .t ;
1212 quantity : quantity ;
1313 unit_price : unit_price ;
14- buySell : buy_sell ;
14+ buy_sell : buy_sell ;
1515 }
1616
17- type order = {
17+ type t = {
1818 no : Orderno .t ;
1919 order_date : Calendar .t ;
2020 account_no : Accountno .t ;
2121 items : line_item list ;
2222 }
2323
24+ let build_line_item order_no isin quantity unit_price buy_sell = {
25+ order_no;
26+ isin;
27+ quantity;
28+ unit_price;
29+ buy_sell;
30+ }
31+
32+ let build_order no order_date account_no items = {
33+ no;
34+ order_date;
35+ account_no;
36+ items;
37+ }
38+
39+ let validate_order ~no ~order_date ~account_no ~items =
40+ let open Validator in
41+ let open Tvalidator in
42+ let valid = build build_order
43+ |> keep no
44+ |> validate order_date (TradingValidator. date_in_future " Order date cannot be in the future" )
45+ |> keep account_no
46+ |> validate items (list_is_not_empty " Line item list cannot be empty" ) in
47+ match valid with
48+ | Ok o -> Ok o
49+ | Error e -> Error e
50+
51+ let validate_line_item ~order_no ~isin ~quantity ~unit_price ~buy_sell =
52+ let open Validator in
53+ let open Tvalidator in
54+ let valid = build build_line_item
55+ |> keep order_no
56+ |> keep isin
57+ |> validate quantity (TradingValidator. float_min 1.0 " Quantity must be > 0" )
58+ |> validate unit_price (TradingValidator. float_min 1.0 " Unit price must be > 0" )
59+ |> keep buy_sell in
60+ match valid with
61+ | Ok li -> Ok li
62+ | Error e -> Error e
63+
64+ let create_line_item ~order_no ~isin ~quantity ~unit_price ~buy_sell =
65+ validate_line_item ~order_no ~isin ~quantity ~unit_price ~buy_sell
66+
67+ let create_order ~no ~order_date ~account_no ~items =
68+ validate_order ~no ~order_date ~account_no ~items
69+
2470end
Original file line number Diff line number Diff line change 1+ open Orderid
2+ open Instrumentid
3+ open Accountid
4+ open Common
5+ open Validator
6+ open CalendarLib
7+
8+ module type Order_sig = sig
9+ (* abstract types *)
10+ type line_item
11+ type t
12+
13+ val create_line_item :
14+ order_no : Orderno .t ->
15+ isin : ISINCode .t ->
16+ quantity : quantity ->
17+ unit_price : unit_price ->
18+ buy_sell : buy_sell ->
19+ (line_item , string ) validator_result
20+
21+ val create_order :
22+ order_no : Orderno .t ->
23+ order_date : Calendar .t ->
24+ account_no : Accountno .t ->
25+ line_items : line_item list ->
26+ (t , string ) validator_result
27+ end
28+
Original file line number Diff line number Diff line change @@ -53,4 +53,13 @@ module TradingValidator = struct
5353 (Calendar. t * Calendar. t option , Calendar. t * Calendar. t option , 'err ) validator_builder =
5454 fun err dates ->
5555 custom date_order_check err dates
56+
57+ let float_min_check (min : float ) (value : float ) =
58+ if value < min then
59+ None
60+ else
61+ Some value
62+
63+
64+ let float_min min = custom (float_min_check min)
5665end
You can’t perform that action at this time.
0 commit comments