@startuml diagram-sequence-create-product

actor       User      as user
participant AppFront as app
participant AppBack as appBack
participant AuthMiddleware as auth
control     ProductController    as ctr
control     ProductService    as srv
control     ApiOpenFoodFact    as apifood
control     ProductRepository   as repo
database DB as db


title Diagramme de séquence: Création d'un produit

autonumber

user -> app : EAN search
activate app

app -> appBack : GET search request
activate appBack
appBack -> auth : Checking token
activate auth

alt #E25A5A NOT AUTHENTICATED

  auth -> app : Response with error code (401)
  app -> user : Display toast error

else NOT AUTHORIZED
  
  auth -> app : Response with error code (403)
  app -> user : Display toast error

end

auth -> ctr : Transfer GET request
deactivate auth
activate ctr

ctr -> srv : Call searchProducts method
activate srv
srv -> repo : Call find methode with EAN code and storeId
activate repo
repo -> db : Execute SQL Query
activate db
db -> repo : Return found product
deactivate db
repo -> srv : Return found product
deactivate repo

alt #E25A5A PRODUCT FOUND

  srv -> appBack : Throw error code (409)
  appBack -> app : Response with error code (409)
  app -> user : Display toast error

end

srv -> apifood : GET request
activate apifood
apifood -> srv : Response
deactivate apifood

alt #E25A5A PRODUCT NOT FOUND

  srv -> appBack : Throw error code (404)
  appBack -> app : Response with error code (404)
  app -> user : Display toast error

end

srv -> ctr : Response
deactivate srv
ctr -> appBack : Response
deactivate ctr
appBack -> app : Response
deactivate appBack

user -> app : Fill form

app -> appBack : POST request
activate appBack
appBack -> auth : Checking token
activate auth

alt #E25A5A NOT AUTHENTICATED

  auth -> app : Response with error code (401)
  app -> user : Display toast error

else NOT AUTHORIZED
  
  auth -> app : Response with error code (403)
  app -> user : Display toast error

end

auth -> ctr : Transfer POST request
deactivate auth
activate ctr
ctr -> ctr : Request validation

alt #E25A5A REQUEST NOT VALID

  ctr -> app : Response with error code (400)
  app -> user : Display toast error

end

ctr -> srv : Call createProduct method
activate srv
srv -> repo : Call create and persist methods
activate repo
repo -> db : Execute SQL query
activate db

alt #759E88 SUCCEED

  db -> repo : Product created
  repo -> srv : Product created
  srv -> ctr : Product created
  ctr -> appBack : Product created
  appBack -> app : Response product created (201)
  app -> user : Redirect to products list

else #E25A5A FAILED

  db -> repo : Product not created
  deactivate db
  repo -> srv : Product not created
  deactivate repo
  srv -> ctr : Product not created
  deactivate srv
  ctr -> appBack : Product not created
  deactivate ctr
  appBack -> app : Response with error code (500)
  deactivate appBack
  app -> user : Display toast error
  deactivate app

end

@enduml

