Skip to content

Commit

Permalink
feat(objects): Introduce components for objects and veli (#825)
Browse files Browse the repository at this point in the history
---------
Co-authored-by: Vincent Jousse <[email protected]>
Co-authored-by: Nicolas Perriault <[email protected]>
  • Loading branch information
vjousse authored Nov 21, 2024
1 parent cb5e131 commit 464d978
Show file tree
Hide file tree
Showing 6 changed files with 454 additions and 168 deletions.
76 changes: 62 additions & 14 deletions public/data/object/examples.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[
{
"id": "0d08a9ff-5683-4d3d-a7b8-dce74f37cd3b",
"name": "Objet par défaut",
"name": "Objet vide",
"scope": "object",
"category": "",
"query": {
"processes": []
"items": []
}
},
{
Expand All @@ -14,7 +14,7 @@
"scope": "veli",
"category": "",
"query": {
"processes": []
"items": [{ "name": "Composant par défaut", "processes": [], "quantity": 0 }]
}
},
{
Expand All @@ -23,14 +23,38 @@
"scope": "object",
"category": "",
"query": {
"processes": [
"items": [
{
"process_id": "07e9e916-e02b-45e2-a298-2b5084de6242",
"amount": 0.00088
"name": "Pied 70 cm (plein bois)",
"processes": [
{
"process_id": "07e9e916-e02b-45e2-a298-2b5084de6242",
"amount": 0.00022
}
],
"quantity": 4
},
{
"process_id": "3295b2a5-328a-4c00-b046-e2ddeb0da823",
"amount": 1.645313
"name": "Dossier plastique (PP)",
"processes": [
{
"process_id": "3295b2a5-328a-4c00-b046-e2ddeb0da823",
"amount": 0.734063
}
],

"quantity": 1
},
{
"name": "Assise plastique (PP)",
"processes": [
{
"process_id": "3295b2a5-328a-4c00-b046-e2ddeb0da823",
"amount": 0.91125
}
],

"quantity": 1
}
]
}
Expand All @@ -41,10 +65,28 @@
"scope": "object",
"category": "",
"query": {
"processes": [
"items": [
{
"name": "Pied 90 cm (plein bois)",
"processes": [
{
"process_id": "07e9e916-e02b-45e2-a298-2b5084de6242",
"amount": 0.007065
}
],

"quantity": 4
},
{
"process_id": "07e9e916-e02b-45e2-a298-2b5084de6242",
"amount": 0.002
"name": "Plateau 200x100 (chêne)",
"processes": [
{
"process_id": "07e9e916-e02b-45e2-a298-2b5084de6242",
"amount": 0.14
}
],

"quantity": 1
}
]
}
Expand All @@ -55,10 +97,16 @@
"scope": "veli",
"category": "",
"query": {
"processes": [
"items": [
{
"process_id": "3295b2a5-328a-4c00-b046-e2ddeb0da823",
"amount": 35
"name": "Cadre plastique",
"processes": [
{
"process_id": "3295b2a5-328a-4c00-b046-e2ddeb0da823",
"amount": 35
}
],
"quantity": 1
}
]
}
Expand Down
139 changes: 94 additions & 45 deletions src/Data/Object/Query.elm
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
module Data.Object.Query exposing
( Amount
, Item
, Component
, ProcessItem
, Quantity
, Query
, amount
, amountToFloat
, b64encode
, buildApiQuery
, decode
, default
, defaultItem
, defaultComponent
, encode
, parseBase64Query
, removeItem
, quantity
, quantityToInt
, removeComponent
, toString
, updateItem
, updateComponent
)

import Base64
import Data.Object.Process as Process exposing (Process)
import Data.Scope as Scope exposing (Scope)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline as Pipe
import Json.Encode as Encode
import Result.Extra as RE
import Url.Parser as Parser exposing (Parser)


type alias Query =
{ items : List Item
{ components : List Component
}


type alias Item =
type alias Component =
{ name : String
, processes : List ProcessItem
, quantity : Quantity
}


type alias ProcessItem =
{ amount : Amount
, processId : Process.Id
}
Expand All @@ -40,16 +51,25 @@ type Amount
= Amount Float


amount : Float -> Amount
amount =
Amount
type Quantity
= Quantity Int


amountToFloat : Amount -> Float
amountToFloat (Amount float) =
float


quantity : Int -> Quantity
quantity int =
Quantity int


quantityToInt : Quantity -> Int
quantityToInt (Quantity int) =
int


buildApiQuery : Scope -> String -> Query -> String
buildApiQuery scope clientUrl query =
"""curl -sS -X POST %apiUrl% \\
Expand All @@ -63,57 +83,77 @@ buildApiQuery scope clientUrl query =

decode : Decoder Query
decode =
Decode.map Query
(Decode.field "processes" (Decode.list decodeItem))
Decode.succeed Query
|> Pipe.required "items" (Decode.list decodeItem)


decodeItem : Decoder Item
decodeItem : Decoder Component
decodeItem =
Decode.map2 Item
Decode.map3 Component
(Decode.field "name" Decode.string)
(Decode.field "processes" (Decode.list decodeProcessItem))
(Decode.field "quantity" (Decode.map Quantity Decode.int))


decodeProcessItem : Decoder ProcessItem
decodeProcessItem =
Decode.map2 ProcessItem
(Decode.field "amount" (Decode.map Amount Decode.float))
(Decode.field "process_id" Process.decodeId)


default : Query
default =
{ items = [] }
{ components = [] }


defaultItem : Process -> Item
defaultItem process =
{ amount = Amount 1, processId = process.id }
defaultComponent : Component
defaultComponent =
{ name = "Composant par défaut"
, processes = []
, quantity = Quantity 1
}


encode : Query -> Encode.Value
encode query =
Encode.object
[ ( "processes"
, Encode.list encodeItem query.items
[ ( "items"
, Encode.list encodeItem query.components
)
]


encodeItem : Item -> Encode.Value
encodeItem : Component -> Encode.Value
encodeItem item =
Encode.object
[ ( "amount", item.amount |> amountToFloat |> Encode.float )
, ( "process_id", Process.encodeId item.processId )
[ ( "name", item.name |> Encode.string )
, ( "processes", item.processes |> Encode.list encodeProcessItem )
, ( "quantity", item.quantity |> quantityToInt |> Encode.int )
]


encodeProcessItem : ProcessItem -> Encode.Value
encodeProcessItem processItem =
Encode.object
[ ( "amount", processItem.amount |> amountToFloat |> Encode.float )
, ( "process_id", Process.encodeId processItem.processId )
]


removeItem : Process.Id -> Query -> Query
removeItem processId query =
{ query | items = query.items |> List.filter (.processId >> (/=) processId) }
removeComponent : String -> Query -> Query
removeComponent name ({ components } as query) =
{ query | components = components |> List.filter (.name >> (/=) name) }


updateItem : Item -> Query -> Query
updateItem newItem query =
updateComponent : Component -> Query -> Query
updateComponent newItem query =
{ query
| items =
query.items
| components =
query.components
|> List.map
(\item ->
if item.processId == newItem.processId then
if item.name == newItem.name then
newItem

else
Expand All @@ -124,23 +164,32 @@ updateItem newItem query =

toString : List Process -> Query -> Result String String
toString processes =
.items
>> List.map
(\item ->
item.processId
|> Process.findById processes
|> Result.map
(\process ->
String.fromFloat (amountToFloat item.amount)
++ process.unit
++ " "
++ process.displayName
)
)
>> RE.combine
.components
>> RE.combineMap (itemToString processes)
>> Result.map (String.join ", ")


itemToString : List Process -> Component -> Result String String
itemToString processes item =
item.processes
|> RE.combineMap (processItemToString processes)
|> Result.map (String.join " | ")
|> Result.map (\processesString -> String.fromInt (quantityToInt item.quantity) ++ " " ++ item.name ++ " [ " ++ processesString ++ " ]")


processItemToString : List Process -> ProcessItem -> Result String String
processItemToString processes processItem =
processItem.processId
|> Process.findById processes
|> Result.map
(\process ->
String.fromFloat (amountToFloat processItem.amount)
++ process.unit
++ " "
++ process.displayName
)



-- Parser

Expand Down
Loading

0 comments on commit 464d978

Please sign in to comment.