-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
- add multitenacy routes
Showing
14 changed files
with
314 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/database/migrations/20241026160050-add-tenantId-to-userProjects.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
/* | ||
Add altering commands here. | ||
Return a promise to correctly handle asynchronicity. | ||
Example: | ||
return queryInterface.createTable('users', { id: Sequelize.INTEGER }); | ||
*/ | ||
return queryInterface.addColumn('UserProjects', 'tenantId', { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'Tenants', | ||
key: 'id' | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'SET NULL' | ||
}) | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
/* | ||
Add reverting commands here. | ||
Return a promise to correctly handle asynchronicity. | ||
Example: | ||
return queryInterface.dropTable('users'); | ||
*/ | ||
return queryInterface.removeColumn('UserProjects', 'tenantId'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Tenants = sequelize.define('Tenants', { | ||
id: { | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
import { Model, DataTypes } from "sequelize"; | ||
import sequelize from "../config/sequelize"; | ||
|
||
class Tenants extends Model { } | ||
|
||
const TenantsModel = () => { | ||
Tenants.init( | ||
{ | ||
id: { | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
primaryKey: true, | ||
unique: true, | ||
allowNull: false, | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
subdomain: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
} | ||
}, | ||
name: DataTypes.STRING, | ||
subdomain: DataTypes.STRING | ||
}, {}); | ||
Tenants.associate = function(models) { | ||
// associations can be defined here | ||
}; | ||
{ | ||
sequelize, | ||
modelName: "Tenants", | ||
tableName: "Tenants", | ||
timestamps: true, | ||
} | ||
); | ||
|
||
return Tenants; | ||
}; | ||
}; | ||
|
||
export default TenantsModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import responses from "../responses"; | ||
|
||
const tenants = { | ||
"/tenants": { | ||
post: { | ||
tags: ["Tenants"], | ||
security: [{ JWT: [] }], | ||
summary: "Create a tenant", | ||
parameters: [ | ||
{ | ||
in: "body", | ||
name: "body", | ||
required: true, | ||
schema: { | ||
example: { | ||
name: "test" | ||
}, | ||
}, | ||
}, | ||
], | ||
consumes: ["application/json"], | ||
responses, | ||
}, | ||
get: { | ||
tags: ["Tenants"], | ||
security: [{ JWT: [] }], | ||
summary: "get all Tenants", | ||
parameters: [], | ||
consumes: ["application/json"], | ||
responses, | ||
}, | ||
}, | ||
"/tenants/{id}": { | ||
get: { | ||
tags: ["Tenants"], | ||
security: [{ JWT: [] }], | ||
summary: "Get tenant by ID", | ||
parameters: [ | ||
{ | ||
in: "path", | ||
name: "id", | ||
required: true, | ||
type: "string", | ||
description: "Tenant ID" | ||
} | ||
], | ||
consumes: ["application/json"], | ||
responses, | ||
}, | ||
put: { | ||
tags: ["Tenants"], | ||
security: [{ JWT: [] }], | ||
summary: "Update a tenant", | ||
parameters: [ | ||
{ | ||
in: "body", | ||
name: "body", | ||
required: true, | ||
schema: { | ||
example: { | ||
name: "test" | ||
}, | ||
}, | ||
} | ||
], | ||
consumes: ["application/json"], | ||
responses, | ||
}, | ||
delete: { | ||
tags: ["Tenants"], | ||
security: [{ JWT: [] }], | ||
summary: "Delete a tenant", | ||
parameters: [ | ||
{ | ||
in: "path", | ||
name: "id", | ||
required: true, | ||
type: "string", | ||
description: "Tenant ID" | ||
} | ||
], | ||
consumes: ["application/json"], | ||
responses, | ||
}, | ||
}, | ||
|
||
}; | ||
|
||
export default tenants; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import DB from "../../database" | ||
import Response from "../../system/helpers/Response" | ||
import createSubdomain from "../../system/utils/createSubdomain" | ||
|
||
const { Tenants } = DB | ||
|
||
|
||
|
||
export default class TenantController { | ||
static async createTenant(req, res) { | ||
const { name } = req?.body | ||
const subdomain = await createSubdomain(name) | ||
console.log({ subdomain }) | ||
try { | ||
const tenant = await Tenants.create({ name, subdomain }) | ||
Response.success(res, 201, { | ||
message: "Tenant created", | ||
tenant | ||
}) | ||
} catch (error) { | ||
Response.error(res, 500, error) | ||
|
||
} | ||
} | ||
|
||
static async getTenants(req, res) { | ||
try { | ||
|
||
const tenants = await Tenants.findAll() | ||
Response.success(res, 200, { | ||
tenants | ||
}) | ||
} catch (error) { | ||
Response.error(res, 500, error) | ||
} | ||
|
||
} | ||
|
||
static async getTenantById(req, res) { | ||
const { id } = req?.params | ||
try { | ||
const tenant = await Tenants.findByPk(id) | ||
Response.success(res, 200, { | ||
tenant | ||
}) | ||
} catch (error) { | ||
Response.error(res, 500, error) | ||
} | ||
} | ||
|
||
static async updateTenant(req, res) { | ||
const { id } = req?.params | ||
const { name } = req?.body | ||
try { | ||
const tenant = await Tenants.update({ name }, { where: { id } }) | ||
Response.success(res, 200, { | ||
message: "Tenant updated", | ||
tenant | ||
}) | ||
} catch (error) { | ||
Response.error(res, 500, error) | ||
} | ||
} | ||
|
||
static async deleteTenant(req, res) { | ||
const { id } = req?.params | ||
try { | ||
const tenant = await Tenants.destroy({ where: { id } }) | ||
Response.success(res, 200, { | ||
message: "Tenant deleted", | ||
tenant | ||
}) | ||
} catch (error) { | ||
Response.error(res, 500, error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Router } from "express"; | ||
import protect from "../middlewares"; | ||
import TenantController from "../controllers/tenantsController"; | ||
import allowedRole from "../middlewares/allowedRoles"; | ||
|
||
const router = Router(); | ||
|
||
router.post("/",protect, TenantController.createTenant); | ||
router.get("/", protect, allowedRole(["admin"]), TenantController.getTenants); | ||
router.get("/:id", protect, TenantController.getTenantById); | ||
router.put("/:id", protect, allowedRole(["admin"]), TenantController.updateTenant); | ||
router.delete("/:id", protect, allowedRole(["admin"]), TenantController.deleteTenant); | ||
|
||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Op } from "sequelize"; | ||
import db from "../database"; | ||
const { Tenants } = db; | ||
|
||
export default class TenantService { | ||
static async findAllTenants(param) { | ||
return await Tenants.findAll() | ||
} | ||
|
||
static async findOneTenant(param) { | ||
return await Tenants.findOne({ | ||
where: param | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import DB from "../../database"; | ||
import TenantService from "../../services/TenantService"; | ||
const { Tenant } = DB; | ||
|
||
export default async (name) => { | ||
|
||
let subdomain = name.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
|
||
|
||
let isUnique = false; | ||
let counter = 1; | ||
|
||
try { | ||
while (!isUnique) { | ||
const existingTenant = await TenantService.findOneTenant({ subdomain }); | ||
|
||
if (!existingTenant) { | ||
isUnique = true; | ||
} else { | ||
|
||
subdomain = `${subdomain}${counter}`; | ||
counter++; | ||
} | ||
} | ||
|
||
} catch (error) { | ||
throw new Error(error); | ||
|
||
} | ||
console.log({ subdomain }); | ||
return subdomain; | ||
}; |