Company Delete Endpoint done
This commit is contained in:
parent
40ac601c2d
commit
dec75bea91
@ -26,6 +26,7 @@ func (c *CompanyUserController) URLMapping() {
|
|||||||
c.Mapping("GetAll", c.GetAll)
|
c.Mapping("GetAll", c.GetAll)
|
||||||
c.Mapping("Put", c.Put)
|
c.Mapping("Put", c.Put)
|
||||||
c.Mapping("Delete", c.Delete)
|
c.Mapping("Delete", c.Delete)
|
||||||
|
c.Mapping("Delete", c.DeleteCompany)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post ...
|
// Post ...
|
||||||
@ -240,7 +241,6 @@ func (c *CompanyUserController) Delete() {
|
|||||||
c.ServeJSONError("Error deleting Company User")
|
c.ServeJSONError("Error deleting Company User")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(userCompanyMapping)
|
|
||||||
|
|
||||||
err = models.DeleteUserCompanyMap(systemO, userCompanyMapping.ID)
|
err = models.DeleteUserCompanyMap(systemO, userCompanyMapping.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -250,3 +250,49 @@ func (c *CompanyUserController) Delete() {
|
|||||||
c.ServeJSONSuccess("Successfully deleted!")
|
c.ServeJSONSuccess("Successfully deleted!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteCompany ...
|
||||||
|
// @Title Delete Company
|
||||||
|
// @Description Delete the entire Company
|
||||||
|
// @Success 200 {string} delete success!
|
||||||
|
// @Failure 403 failed
|
||||||
|
// @router /deletecompany [delete]
|
||||||
|
func (c *CompanyUserController) DeleteCompany() {
|
||||||
|
|
||||||
|
if currentUser.Role != constants.RoleOwner {
|
||||||
|
c.ServeJSONError("Must be Owner to delete a company")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
systemDB := companydb.GetSystemDatabase()
|
||||||
|
systemO, err := orm.NewOrmWithDB("postgres", "default", systemDB)
|
||||||
|
|
||||||
|
// first check how many users are left
|
||||||
|
ucm, err := models.GetUserCompanyMapsByCompanyName(systemO, jwtSession.CompanyName)
|
||||||
|
if err != nil {
|
||||||
|
c.ServeJSONError("Error deleting User Company Relation")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, uc := range ucm {
|
||||||
|
systemO.Begin()
|
||||||
|
err = models.DeleteUserCompanyMap(systemO, uc.ID)
|
||||||
|
if err != nil {
|
||||||
|
c.ServeJSONError("Error deleting User Company Relation")
|
||||||
|
systemO.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second check and delete the database
|
||||||
|
err = companydb.DeleteDatabase(jwtSession.CompanyName)
|
||||||
|
if err != nil {
|
||||||
|
systemO.Rollback()
|
||||||
|
c.ServeJSONError("Error deleting Company Database")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
systemO.Commit()
|
||||||
|
c.ServeJSONSuccess("Successfully deleted!")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -140,6 +140,18 @@ func GetAllUserCompanyMap(o orm.Ormer, query map[string]string, fields []string,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUserCompanyMapsByCompanyName retrieves all UserCompanyMap matches a certain company. Returns empty list if
|
||||||
|
// no records exist
|
||||||
|
func GetUserCompanyMapsByCompanyName(o orm.Ormer, companyName string) (l []UserCompanyMap, err error) {
|
||||||
|
qs := o.QueryTable(new(UserCompanyMap)).Filter("company", companyName)
|
||||||
|
|
||||||
|
_, err = qs.All(&l)
|
||||||
|
if err == nil {
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateUserCompanyMapById updates UserCompanyMap by Id and returns error if the record to be updated doesn't exist
|
// UpdateUserCompanyMapById updates UserCompanyMap by Id and returns error if the record to be updated doesn't exist
|
||||||
func UpdateUserCompanyMapById(o orm.Ormer, m *UserCompanyMap) (err error) {
|
func UpdateUserCompanyMapById(o orm.Ormer, m *UserCompanyMap) (err error) {
|
||||||
v := UserCompanyMap{ID: m.ID}
|
v := UserCompanyMap{ID: m.ID}
|
||||||
|
@ -103,6 +103,14 @@ func init() {
|
|||||||
MethodParams: param.Make(),
|
MethodParams: param.Make(),
|
||||||
Params: nil})
|
Params: nil})
|
||||||
|
|
||||||
|
beego.GlobalControllerRouter["multitenantStack/controllers:CompanyUserController"] = append(beego.GlobalControllerRouter["multitenantStack/controllers:CompanyUserController"],
|
||||||
|
beego.ControllerComments{
|
||||||
|
Method: "DeleteCompany",
|
||||||
|
Router: `/deletecompany`,
|
||||||
|
AllowHTTPMethods: []string{"delete"},
|
||||||
|
MethodParams: param.Make(),
|
||||||
|
Params: nil})
|
||||||
|
|
||||||
beego.GlobalControllerRouter["multitenantStack/controllers:ContactController"] = append(beego.GlobalControllerRouter["multitenantStack/controllers:ContactController"],
|
beego.GlobalControllerRouter["multitenantStack/controllers:ContactController"] = append(beego.GlobalControllerRouter["multitenantStack/controllers:ContactController"],
|
||||||
beego.ControllerComments{
|
beego.ControllerComments{
|
||||||
Method: "Post",
|
Method: "Post",
|
||||||
|
Loading…
Reference in New Issue
Block a user