From 233c15ad535dffa88d14d52526b660bd28a01e5b Mon Sep 17 00:00:00 2001
From: Dave Kaminski <davekaminski@gmail.com>
Date: Mon, 30 Aug 2021 13:20:48 -0700
Subject: [PATCH] feat(drivers): EN-7633 Update drivers (#101)

* feat(drivers): EN-7633 Update drivers

* (+semver:fix) feat: added endpoint for creating a new driver

* Revert "(+semver:fix) feat: added endpoint for creating a new driver"

This reverts commit 70ec9e03ab63c6384123259f7214984bbf5b1722.

Co-authored-by: francisminu <mfrancis@syncromatics.com>
---
 src/mocks/drivers.js         |  8 +++++++-
 src/resources/Driver.js      | 23 ++++++++++++++++++-----
 src/resources/Driver.test.js | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/src/mocks/drivers.js b/src/mocks/drivers.js
index c500fac..4ec8003 100644
--- a/src/mocks/drivers.js
+++ b/src/mocks/drivers.js
@@ -11,11 +11,17 @@ const drivers = {
         },
       });
     const singleResponse = () => new Response(Client.toBlob(drivers.getById(1)));
+    const putResponse = () => new Response(undefined, {
+      headers: {
+        Location: '/1/SYNC/drivers/1',
+      },
+    });
 
     fetchMock
       .get(client.resolve('/1/SYNC/drivers?page=1&per_page=10&sort='), listResponse)
       .get(client.resolve('/1/SYNC/drivers?page=1&per_page=10&q=charlie&sort='), listResponse)
-      .get(client.resolve('/1/SYNC/drivers/1'), singleResponse);
+      .get(client.resolve('/1/SYNC/drivers/1'), singleResponse)
+      .put(client.resolve('/1/SYNC/drivers/1'), putResponse);
   },
   getById: id => drivers.list.find(v => v.id === id),
   list: [{
diff --git a/src/resources/Driver.js b/src/resources/Driver.js
index fd1c0ff..0c40c2c 100644
--- a/src/resources/Driver.js
+++ b/src/resources/Driver.js
@@ -23,11 +23,11 @@ class Driver extends Resource {
    * @param {Client} client Instance of pre-configured client
    * @param {Array} rest Remaining arguments to use in assigning values to this instance
    */
-  constructor(client, ...rest) {
+  constructor(client, rest) {
     super(client);
-
-    const newProperties = Object.assign({}, ...rest);
-    const hydrated = !Object.keys(newProperties).every(k => k === 'href');
+    const { code, ...newProperties } = rest;
+    this.customerCode = code;
+    const hydrated = !Object.keys(newProperties).every(k => k === 'href' || k === 'customerCode');
     Object.assign(this, newProperties, { hydrated });
   }
 
@@ -40,6 +40,7 @@ class Driver extends Resource {
   static makeHref(customerCode, id) {
     return {
       href: `/1/${customerCode}/drivers/${id}`,
+      code: customerCode,
     };
   }
 
@@ -50,7 +51,19 @@ class Driver extends Resource {
   fetch() {
     return this.client.get(this.href)
       .then(response => response.json())
-      .then(driver => new Driver(this.client, this, driver));
+      .then(driver => new Driver(this.client, { ...this, ...driver }));
+  }
+
+  /**
+   * Updates data for a driver via the client
+   * @returns {Promise} If successful, returns instance of this driver
+   */
+  update() {
+    // eslint-disable-next-line no-unused-vars
+    const { client, hydrated, customerCode, ...body } = this;
+    const { href } = Driver.makeHref(this.customerCode, this.id);
+    return this.client.put(href, { body })
+      .then(() => new Driver(this.client, { ...this }));
   }
 }
 
diff --git a/src/resources/Driver.test.js b/src/resources/Driver.test.js
index db79a8b..85b5aa8 100644
--- a/src/resources/Driver.test.js
+++ b/src/resources/Driver.test.js
@@ -50,3 +50,35 @@ describe('When fetching a driver based on customer and ID', () => {
   it('should have the expected first name', () => promise.then(p => p.first_name).should.eventually.equal('Charlie'));
   it('should have the expected last name', () => promise.then(p => p.last_name).should.eventually.equal('Singh'));
 });
+
+describe('When updating a driver for a customer', () => {
+  const client = new Client();
+
+  beforeEach(() => mockDrivers.setUpSuccessfulMock(client));
+  beforeEach(() => fetchMock.catch(503));
+  afterEach(fetchMock.restore);
+
+  let promise;
+  beforeEach(() => {
+    promise = new Driver(client, Driver.makeHref('SYNC', 1))
+      .fetch()
+      .then((driver) => {
+        /* eslint-disable no-param-reassign */
+        driver.customer_driver_id = '0002';
+        driver.first_name = 'Charlotte';
+        driver.last_name = 'Song';
+        /* eslint-enable no-param-reassign */
+        return driver.update();
+      })
+      .then(driver => driver);
+  });
+
+  it('should resolve the promise', () => promise.should.be.fulfilled);
+  it('should set the href', () => promise.then(p => p.href).should.eventually.equal('/1/SYNC/drivers/1'));
+  it('should have the expected customer driver id', () => promise.then(p => p.customer_driver_id)
+    .should.eventually.equal('0002'));
+  it('should have the expected first name', () => promise.then(p => p.first_name)
+    .should.eventually.equal('Charlotte'));
+  it('should have the expected last name', () => promise.then(p => p.last_name)
+    .should.eventually.equal('Song'));
+});