diff --git a/app/Controllers/Test/TestMapController.php b/app/Controllers/Test/TestMapController.php index f8a76d8..c97e98d 100755 --- a/app/Controllers/Test/TestMapController.php +++ b/app/Controllers/Test/TestMapController.php @@ -60,9 +60,9 @@ class TestMapController extends BaseController { return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200); } - public function show($id = null) { - $row = $this->model->where('TestMapID',$id)->where('EndDate', null)->first(); - if (empty($row)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200); } + public function show($id = null) { + $row = $this->model->getByIdWithNames($id); + if (empty($row)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200); } $row = ValueSet::transformLabels([$row], [ 'HostType' => 'entity_type', diff --git a/app/Models/Test/TestMapModel.php b/app/Models/Test/TestMapModel.php index de3c0e1..7600a83 100755 --- a/app/Models/Test/TestMapModel.php +++ b/app/Models/Test/TestMapModel.php @@ -35,7 +35,7 @@ class TestMapModel extends BaseModel { /** * Get all test mappings with names */ - public function getUniqueGroupings() { + public function getUniqueGroupings() { $db = $this->db; $types = $this->getEntityTypes(); @@ -72,15 +72,61 @@ class TestMapModel extends BaseModel { WHERE tm.EndDate IS NULL "; - return $db->query($sql, [ - $siteType, $wsType, $instType, - $siteType, $wsType, $instType, - $siteType, $wsType, $instType, - $siteType, $wsType, $instType - ])->getResultArray(); - } - - public function getMappingsByTestCode($testCode) { + return $db->query($sql, [ + $siteType, $wsType, $instType, + $siteType, $wsType, $instType, + $siteType, $wsType, $instType, + $siteType, $wsType, $instType + ])->getResultArray(); + } + + public function getByIdWithNames($id) { + $db = $this->db; + $types = $this->getEntityTypes(); + + $siteType = $types['SITE'] ?? 'SITE'; + $wsType = $types['WST'] ?? 'WST'; + $instType = $types['INST'] ?? 'INST'; + + $sql = " + SELECT + tm.*, + CASE + WHEN tm.HostType = ? THEN s.SiteName + WHEN tm.HostType = ? THEN ws.WorkstationName + WHEN tm.HostType = ? THEN el.InstrumentName + ELSE tm.HostID + END as HostName, + CASE + WHEN tm.ClientType = ? THEN cs.SiteName + WHEN tm.ClientType = ? THEN cws.WorkstationName + WHEN tm.ClientType = ? THEN cel.InstrumentName + ELSE tm.ClientID + END as ClientName + FROM testmap tm + LEFT JOIN site s ON tm.HostType = ? AND s.SiteID = tm.HostID + LEFT JOIN workstation ws ON tm.HostType = ? AND ws.WorkstationID = tm.HostID + LEFT JOIN equipmentlist el ON tm.HostType = ? AND el.EID = tm.HostID + LEFT JOIN site cs ON tm.ClientType = ? AND cs.SiteID = tm.ClientID + LEFT JOIN workstation cws ON tm.ClientType = ? AND cws.WorkstationID = tm.ClientID + LEFT JOIN equipmentlist cel ON tm.ClientType = ? AND cel.EID = tm.ClientID + WHERE tm.TestMapID = ? + AND tm.EndDate IS NULL + LIMIT 1 + "; + + $rows = $db->query($sql, [ + $siteType, $wsType, $instType, + $siteType, $wsType, $instType, + $siteType, $wsType, $instType, + $siteType, $wsType, $instType, + $id, + ])->getResultArray(); + + return $rows[0] ?? null; + } + + public function getMappingsByTestCode($testCode) { return $this->select('testmap.*') ->join('testmapdetail', 'testmapdetail.TestMapID = testmap.TestMapID', 'inner') ->groupStart() diff --git a/tests/feature/Test/TestMapPatchTest.php b/tests/feature/Test/TestMapPatchTest.php index b9341d4..af1967d 100755 --- a/tests/feature/Test/TestMapPatchTest.php +++ b/tests/feature/Test/TestMapPatchTest.php @@ -53,7 +53,11 @@ class TestMapPatchTest extends CIUnitTestCase $show = $this->withHeaders($this->authHeaders())->call('get', "{$this->endpoint}/{$id}"); $show->assertStatus(200); - return json_decode($show->getJSON(), true)['data']; + $showData = json_decode($show->getJSON(), true)['data']; + $this->assertArrayHasKey('HostName', $showData); + $this->assertArrayHasKey('ClientName', $showData); + + return $showData; } public function testPartialUpdateTestMapSuccess()