From c4b0c98c9a54e3415fb555876fd755b420abab7f Mon Sep 17 00:00:00 2001
From: irfanamal <irfan.ihsanulamal@yahoo.com>
Date: Mon, 15 Apr 2019 19:02:29 +0700
Subject: [PATCH] Update model for organsisasi

---
 .../20190415113534-create-organisasi.js       | 30 +++++++++++++++++++
 ...0190415115145-associate-user-organisasi.js | 19 ++++++++++++
 backend/models/organisasi.js                  | 11 +++++++
 backend/models/user.js                        |  1 +
 4 files changed, 61 insertions(+)
 create mode 100644 backend/migrations/20190415113534-create-organisasi.js
 create mode 100644 backend/migrations/20190415115145-associate-user-organisasi.js
 create mode 100644 backend/models/organisasi.js

diff --git a/backend/migrations/20190415113534-create-organisasi.js b/backend/migrations/20190415113534-create-organisasi.js
new file mode 100644
index 0000000..08f2cab
--- /dev/null
+++ b/backend/migrations/20190415113534-create-organisasi.js
@@ -0,0 +1,30 @@
+'use strict';
+module.exports = {
+  up: (queryInterface, Sequelize) => {
+    return queryInterface.createTable('organisasis', {
+      id: {
+        allowNull: false,
+        autoIncrement: true,
+        primaryKey: true,
+        type: Sequelize.INTEGER
+      },
+      name: {
+        type: Sequelize.STRING
+      },
+      jenis: {
+        type: Sequelize.STRING
+      },
+      createdAt: {
+        allowNull: false,
+        type: Sequelize.DATE
+      },
+      updatedAt: {
+        allowNull: false,
+        type: Sequelize.DATE
+      }
+    });
+  },
+  down: (queryInterface, Sequelize) => {
+    return queryInterface.dropTable('organisasis');
+  }
+};
\ No newline at end of file
diff --git a/backend/migrations/20190415115145-associate-user-organisasi.js b/backend/migrations/20190415115145-associate-user-organisasi.js
new file mode 100644
index 0000000..3d92325
--- /dev/null
+++ b/backend/migrations/20190415115145-associate-user-organisasi.js
@@ -0,0 +1,19 @@
+'use strict';
+
+module.exports = {
+  up: (queryInterface, Sequelize) => {
+    return queryInterface.addColumn('users', 'organisasiId', {
+     type: Sequelize.INTEGER,
+     references: {
+       model: 'organisasis',
+       key: 'id'
+     },
+     onUpdate: 'CASCADE',
+     onDelete: 'SET NULL'
+   });
+  },
+
+  down: (queryInterface, Sequelize) => {
+    return queryInterface.removeColumn('users', 'organisasiId');
+  }
+};
diff --git a/backend/models/organisasi.js b/backend/models/organisasi.js
new file mode 100644
index 0000000..d1dff0a
--- /dev/null
+++ b/backend/models/organisasi.js
@@ -0,0 +1,11 @@
+'use strict';
+module.exports = (sequelize, DataTypes) => {
+  const organisasi = sequelize.define('organisasi', {
+    name: DataTypes.STRING,
+    jenis: DataTypes.STRING
+  }, {});
+  organisasi.associate = function(models) {
+    // associations can be defined here
+  };
+  return organisasi;
+};
\ No newline at end of file
diff --git a/backend/models/user.js b/backend/models/user.js
index 93a0213..cc5cdb7 100644
--- a/backend/models/user.js
+++ b/backend/models/user.js
@@ -8,6 +8,7 @@ module.exports = (sequelize, DataTypes) => {
   }, {});
   user.associate = function(models) {
     models.user.belongsTo(models.role);
+    models.user.belongsTo(models.organisasi);
   };
   return user;
 };
\ No newline at end of file
-- 
GitLab