Skip to main content

Wizard

The flow of the wizard is located in the folder handler/wizard_handler.go. Inside, there is a route /finalize handled by the Finalize function. This function is responsible for processing data related to Departments and Devices (Device).

Handler Process

  1. Route /finalize:

    • Handle by Finalize function.
  2. Finalize Function:

    This function, Finalize, is a handler method for processing a "wizard finalization" request in an HTTP context (using the Echo framework). It performs the following steps:

    • Request Binding and Validation: Binds and validates the incoming request to a FinalizeWizardRequest structure.

    • Transactional Execution: Executes database operations within a transaction:

    • Inserts departments in bulk if the request contains department data.

    • Inserts devices in bulk if the request contains device data.

    • Error Handling: Returns an error if any part of the transaction fails.

    • Success Response: Sends a JSON response indicating success if the operation completes without errors.

    • Processing data for department and device.
    • All data inserted into database using transaction (trx).


    func (h *WizardHandler) Finalize(c echo.Context) error {

    req := &entity.FinalizeWizardRequest{}

    if err := bindAndValidate(c, req); err != nil {

    return err

    }

    txErr := h.Dao.RunInTransaction(func(txDao *daos.Dao) error {

    if len(req.Departments.Items) > 0 {

    if err := queries.BulkInsertDepartments(txDao, req.Departments); err != nil {

    return err

    }

    }

    if len(req.Devices.Items) > 0 {

    if err := queries.BulkInsertDevices(txDao, req.Devices); err != nil {

    return err

    }

    }

    return nil

    })

    if txErr != nil {

    return txErr

    }

    return response.Json(c).Ok(nil)

    }

  3. Functions Involved:

    • queries.BulkInsertDepartments
    • queries.BulkInsertDevices

    Both of these functions are in a folder queries and is in the file department_query.go and device_query.go.


    func InsertDepartment(dao *daos.Dao, req *entity.CreateDepartmentRequest) error {
    table := &entity.DepartmentTable{}

    if err := medaquery.Insert(dao, table.TableName(), map[string]interface{}{
    "company_id": req.CompanyID,
    "label": req.Label,
    }); err != nil {
    return err
    }

    return nil
    }

    func BulkInsertDepartments(dao *daos.Dao, req *entity.BulkCreateDepartmentRequest) error {
    for _, v := range req.Items {
    if err := InsertDepartment(dao, v); err != nil {
    return err
    }
    }

    return nil
    }


    func BulkInsertDevices(dao *daos.Dao, req *entity.BulkCreateDeviceRequest) error {

    for _, v := range req.Items {

    if err := InsertDevice(dao, v.ToDeviceTable()); err != nil {

    return err

    }

    }

    return nil

    }

    func InsertDevice(dao *daos.Dao, req *entity.DeviceTable) error {

    data := utils.ToMap(req)

    data["deleted"] = nil

    if err := medaquery.Insert(dao, req.TableName(), data); err != nil {

    return err

    }

    return nil

    }

  4. Client Response:

    • If process succeed, response will be successful.

Flow Diagram

+-------------------+
| Client |
+--------+----------+
|
v
+--------+----------+
| Route: /finalize |
+--------+----------+
|
v
+--------+----------+
| Function: Finalize|
+--------+----------+
|
v
+-------------------+
| Process Data |
| - Departments |
| - Devices |
+--------+----------+
|
v
+---------------------------+
| Database (trx) |
| - BulkInsertDepartments |
| - BulkInsertDevices |
+--------+------------------+
|
v
+-------------------+
| Response: Success |
+-------------------+