User delete endpoint
This commit is contained in:
		@@ -3,16 +3,18 @@ package controllers
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"multitenantStack/constants"
 | 
			
		||||
	"multitenantStack/models"
 | 
			
		||||
	"multitenantStack/services/companydb"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/astaxie/beego"
 | 
			
		||||
	"github.com/astaxie/beego/orm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// CompanyUserController operations for CompanyUser
 | 
			
		||||
type CompanyUserController struct {
 | 
			
		||||
	beego.Controller
 | 
			
		||||
	BaseAPIController
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// URLMapping ...
 | 
			
		||||
@@ -162,10 +164,37 @@ func (c *CompanyUserController) Put() {
 | 
			
		||||
func (c *CompanyUserController) Delete() {
 | 
			
		||||
	idStr := c.Ctx.Input.Param(":id")
 | 
			
		||||
	id, _ := strconv.Atoi(idStr)
 | 
			
		||||
 | 
			
		||||
	if currentUser.Id != id {
 | 
			
		||||
		c.ServeJSONError("You can not delete users other than yourself!")
 | 
			
		||||
		c.ServeJSON()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if currentUser.Role == constants.RoleOwner {
 | 
			
		||||
		c.ServeJSONError("You can not delete users other than yourself!")
 | 
			
		||||
		c.ServeJSON()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := models.DeleteCompanyUser(o, id); err == nil {
 | 
			
		||||
		c.Data["json"] = "OK"
 | 
			
		||||
	} else {
 | 
			
		||||
		c.Data["json"] = err.Error()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// After deleting the user here we need to delete the same User in the system DB
 | 
			
		||||
 | 
			
		||||
	userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, jwtSession.Email)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.ServeJSONError("Error deleting Company User")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	systemDB := companydb.GetSystemDatabase()
 | 
			
		||||
	systemO, err := orm.NewOrmWithDB("postgres", "default", systemDB)
 | 
			
		||||
	err = models.DeleteUserCompanyMap(systemO, userCompanyMapping.ID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.ServeJSONError("Error deleting User Company Relation")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.ServeJSON()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user