From 17dfaad0f715260db0d3c5b04d27086a776806ce Mon Sep 17 00:00:00 2001 From: ManujVieira Date: Tue, 30 Aug 2016 18:37:14 +0100 Subject: [PATCH] feat: Added method to retrieve task by Id --- tasks.go | 43 +++++++++++++----- tasks/models/checklist_task_item.go | 12 ++--- tasks/models/product_task_item.go | 18 ++++---- tasks/models/request.go | 5 +++ tasks/models/task.go | 2 +- tasks/models/task_item.go | 69 +++++++++++++++++++++++++---- tasks_test.go | 58 ++++++++++++++++-------- 7 files changed, 154 insertions(+), 53 deletions(-) diff --git a/tasks.go b/tasks.go index f093faa..41b68f0 100644 --- a/tasks.go +++ b/tasks.go @@ -1,6 +1,5 @@ package mrs_service_go_sdk - import ( "net/http" "fmt" @@ -8,15 +7,15 @@ import ( "github.com/Tlantic/mrs-service-go-sdk/tasks/models" ) -const TASKS_MODULE_NAME = "tasks" +const TASKS_MODULE_NAME = "tasks" // Retrieves a task summary func (c *Client) Summary() (*models.TaskSummary, error) { p := models.TaskSummary{} - url := fmt.Sprintf("%s/%s", c.BaseApi, createPath(c.Organization, c.Application,"summary")) + url := fmt.Sprintf("%s/%s", c.BaseApi, createPath(c.Organization, c.Application, "summary")) - req, err := http.NewRequest("GET", url , nil) + req, err := http.NewRequest("GET", url, nil) req.Header.Set("mrs-application-id", c.AppId) if err != nil { @@ -28,7 +27,6 @@ func (c *Client) Summary() (*models.TaskSummary, error) { return &p, err } - return &p, nil } @@ -40,7 +38,7 @@ func (c *Client) GetTasks(items bool) (*models.TasksRequest, error) { url := fmt.Sprintf("%s/%s", c.BaseApi, createPath(c.Organization, c.Application, fmt.Sprintf("?items=%t", items))) - req, err := http.NewRequest("GET", url , nil) + req, err := http.NewRequest("GET", url, nil) req.Header.Set("mrs-application-id", c.AppId) if err != nil { @@ -52,17 +50,43 @@ func (c *Client) GetTasks(items bool) (*models.TasksRequest, error) { return &p, err } + return &p, nil +} + +func (c *Client) GetTaskById(taskUId string, items bool) (*models.TaskRequest, error) { + p := models.TaskRequest{} + + url := fmt.Sprintf("%s/%s", c.BaseApi, createPath(c.Organization, c.Application, fmt.Sprintf("%s?items=%t", taskUId, items))) + + fmt.Print(url) + + req, err := http.NewRequest("GET", url, nil) + req.Header.Set("mrs-application-id", c.AppId) + + if err != nil { + return &p, err + } + + err = c.Send(req, &p) + if err != nil { + return &p, err + } + if (p.Result.TaskType == "checklist") { + p.Result.ItemType = models.ChecklistTaskItem{} + } else { + p.Result.ItemType = models.ProductTaskItem{} + } return &p, nil } // Create a task and its items. -func (c *Client) CreateTask( task *models.Task ) (*models.TasksRequest, error) { +func (c *Client) CreateTask(task *models.Task) (*models.TasksRequest, error) { p := models.TasksRequest{} url := fmt.Sprintf("%s/%s", c.BaseApi, createPath(c.Organization, c.Application, "")) - req, err := http.NewRequest("POST", url , nil) + req, err := http.NewRequest("POST", url, nil) req.Header.Set("mrs-application-id", c.AppId) if err != nil { @@ -74,11 +98,10 @@ func (c *Client) CreateTask( task *models.Task ) (*models.TasksRequest, error) { return &p, err } - return &p, nil } -func createPath(org, app, extra string) string{ +func createPath(org, app, extra string) string { return fmt.Sprintf("%s/orgs/%s/apps/%s/%s/%s", TASKS_MODULE_NAME, org, app, TASKS_MODULE_NAME, extra) } diff --git a/tasks/models/checklist_task_item.go b/tasks/models/checklist_task_item.go index a267082..4648fd5 100644 --- a/tasks/models/checklist_task_item.go +++ b/tasks/models/checklist_task_item.go @@ -1,6 +1,6 @@ package models -type ChecklistTaskItem_v1 struct { +type ChecklistTaskItem struct { UniqueId string `json:"_uId"` ApplicationId string `json:"applicationId"` @@ -15,22 +15,22 @@ type ChecklistTaskItem_v1 struct { } -func ( item *ChecklistTaskItem_v1 ) GetUniqueId() string { +func ( item *ChecklistTaskItem ) GetUniqueId() string { return item.UniqueId } -func ( item *ChecklistTaskItem_v1 ) GetItemId() string { +func ( item *ChecklistTaskItem ) GetItemId() string { return item.ItemId } -func ( item *ChecklistTaskItem_v1 ) GetType() string { +func ( item *ChecklistTaskItem ) GetType() string { return item.Type } -func ( item *ChecklistTaskItem_v1 ) GetStatus() string { +func ( item *ChecklistTaskItem ) GetStatus() string { return item.Status } -func ( item *ChecklistTaskItem_v1 ) GetStartDate() int { +func ( item *ChecklistTaskItem ) GetStartDate() int { return item.StartDate } diff --git a/tasks/models/product_task_item.go b/tasks/models/product_task_item.go index 8e66673..9b870ad 100644 --- a/tasks/models/product_task_item.go +++ b/tasks/models/product_task_item.go @@ -2,7 +2,7 @@ package models import "github.com/Tlantic/mrs-service-go-sdk/catalog/models" -type ProductTaskItem_v1 struct { +type ProductTaskItem struct { UniqueId string `json:"_uId"` ApplicationId string `json:"applicationId"` @@ -40,30 +40,30 @@ type ProductTaskItem_v1 struct { } -func ( item *ProductTaskItem_v1 ) GetUniqueId() string { - return item.UniqueId +func ( item *ProductTaskItem ) GetUniqueId() string { +return item.UniqueId } -func ( item *ProductTaskItem_v1 ) GetItemId() string { +func ( item *ProductTaskItem ) GetItemId() string { return item.ItemId } -func ( item *ProductTaskItem_v1 ) GetItemEAN() string { +func ( item *ProductTaskItem ) GetItemEAN() string { return item.ItemEAN } -func ( item *ProductTaskItem_v1 ) GetName() string { +func ( item *ProductTaskItem ) GetName() string { return item.ItemEAN } -func ( item *ProductTaskItem_v1 ) GetType( ) string { +func ( item *ProductTaskItem ) GetType( ) string { return item.Type } -func ( item *ProductTaskItem_v1 ) GetStatus() string { +func ( item *ProductTaskItem ) GetStatus() string { return item.Status } -func ( item *ProductTaskItem_v1 ) GetStartDate() int { +func ( item *ProductTaskItem ) GetStartDate() int { return item.StartDate } diff --git a/tasks/models/request.go b/tasks/models/request.go index c9d7f75..d5939f8 100644 --- a/tasks/models/request.go +++ b/tasks/models/request.go @@ -3,4 +3,9 @@ package models type TasksRequest struct { Status string `json:"status"` Result []Task `json:"result"` +} + +type TaskRequest struct { + Status string `json:"status"` + Result Task `json:"result"` } \ No newline at end of file diff --git a/tasks/models/task.go b/tasks/models/task.go index 1491e73..5f6055d 100644 --- a/tasks/models/task.go +++ b/tasks/models/task.go @@ -27,5 +27,5 @@ type Task struct { CreateDate int `json:"_createDate"` UpdateUser string `json:"_updateUser"` CreateUser string `json:"_createUser"` - + ItemType interface{} `json:"itemType"` } \ No newline at end of file diff --git a/tasks/models/task_item.go b/tasks/models/task_item.go index 41774b7..e853fc6 100644 --- a/tasks/models/task_item.go +++ b/tasks/models/task_item.go @@ -1,10 +1,63 @@ package models -type TaskItem interface { - GetUniqueId() string - GetItemId() string - GetName() string - GetType() string - GetStatus() string - GetStartDate() int -} \ No newline at end of file +import "github.com/Tlantic/mrs-service-go-sdk/catalog/models" + +type TaskItem struct { + UniqueId string `json:"_uId"` + ApplicationId string `json:"applicationId"` + TaskId string `json:"taskId"` + Type string `json:"type"` + Status string `json:"status"` + Observation string `json:"observation"` + StartDate int `json:"startDate"` + + ItemId string `json:"itemId"` + ItemEAN string `json:"itemEAN"` + ItemName string `json:"itemName"` + ExpirationDate int `json:"expirationDate"` + HierarchyCategory models.HierarchyCategory `json:"hierarchyCategory"` + + PosPrice int `json:"posPrice"` + LabelPrice int `json:"labelPrice"` + OldERPPrice int `json:"oldERPPrice"` + PriceDivergence bool `json:"priceDivergence"` + + Quantity int `json:"quantity"` + ExpectedQuantity int `json:"expectedQuantity"` + + CheckExpirationDate bool `json:"checkExpirationDate"` + + PickCount int `json:"pickCount"` + FirstPickingTime int `json:"firstPickingTime"` + ReceptionUnit int `json:"receptionUnit"` + ReceptionUnitQuantity int `json:"receptionUnitQuantity"` +} + +func ( item *TaskItem ) GetUniqueId() string { + return item.UniqueId +} + +func ( item *TaskItem ) GetItemId() string { + return item.ItemId +} + +func ( item *TaskItem ) GetItemEAN() string { + return item.ItemEAN +} + +func ( item *TaskItem ) GetName() string { + return item.ItemEAN +} + +func ( item *TaskItem ) GetType() string { + return item.Type +} + +func ( item *TaskItem ) GetStatus() string { + return item.Status +} + +func ( item *TaskItem ) GetStartDate() int { + return item.StartDate +} + diff --git a/tasks_test.go b/tasks_test.go index dd6a112..38645f5 100644 --- a/tasks_test.go +++ b/tasks_test.go @@ -5,73 +5,93 @@ import ( "github.com/Tlantic/mrs-service-go-sdk/tasks/models" ) - func TestTaskSummary(t *testing.T) { - cl, err := NewClient("http://52.50.91.27:8067","tlantic", "instore", "489a598e-259c-4e25-974e-5de00b29f707") + cl, err := NewClient("http://52.50.91.27:8067", "tlantic", "instore", "489a598e-259c-4e25-974e-5de00b29f707") if err != nil { - t.Errorf( err.Error()) + t.Errorf(err.Error()) } summary, err := cl.Summary() - if err != nil{ - t.Errorf( err.Error()) + if err != nil { + t.Errorf(err.Error()) } t.Log(summary.Result.PriceChang.Total) - } - func TestGetTasks(t *testing.T) { - cl, err := NewClient("http://52.50.91.27:8067","tlantic", "instore", "489a598e-259c-4e25-974e-5de00b29f707") + cl, err := NewClient("http://52.50.91.27:8067", "tlantic", "instore", "489a598e-259c-4e25-974e-5de00b29f707") if err != nil { - t.Errorf( err.Error()) + t.Errorf(err.Error()) } tasks, err := cl.GetTasks(true) - if err != nil{ - t.Errorf( err.Error()) + if err != nil { + t.Errorf(err.Error()) } t.Log(tasks.Result) - if(len(tasks.Result)>0){ + if (len(tasks.Result) > 0) { task := tasks.Result[0] t.Log(task.TaskName) t.Log(task.Items) } - } +func TestGetTaskById(t *testing.T) { + + cl, err := NewClient("http://52.50.91.27:8067", "tlantic", "instore", "489a598e-259c-4e25-974e-5de00b29f707") + if err != nil { + t.Errorf(err.Error()) + } + + tasks, err := cl.GetTaskById("589f3517-0c98-4071-9fea-ab3e91dbe7ec", true) + + if err != nil { + t.Errorf(err.Error()) + } + + t.Log(tasks.Result) + + task := tasks.Result + t.Log(task.TaskName) + t.Log(task.Items) + + for _, item := range task.Items { + t.Log(models.ProductTaskItem(item)) + } + + t.Log(task.Items) +} func TestCreateTask(t *testing.T) { - cl, err := NewClient("http://52.50.91.27:8067","tlantic", "instore", "489a598e-259c-4e25-974e-5de00b29f707") + cl, err := NewClient("http://52.50.91.27", "tlantic", "instore", "") if err != nil { - t.Errorf( err.Error()) + t.Errorf(err.Error()) } tasks, err := cl.CreateTask(&models.Task{ }) - if err != nil{ - t.Errorf( err.Error()) + if err != nil { + t.Errorf(err.Error()) } t.Log(tasks.Result) - if(len(tasks.Result)>0){ + if (len(tasks.Result) > 0) { task := tasks.Result[0] t.Log(task.TaskName) t.Log(task.Items) } - }