Update API documentation to reflect customer authorization changes
- Changed tags from "Customers-Mobile" to "Customers-Authorization" in Swagger documentation for customer logout and profile retrieval endpoints. - Updated the Company entity to replace CustomerID with OwnerCustomerID to better represent ownership. - Removed obsolete customer assignment methods and related forms from the company domain, streamlining the codebase. - Adjusted customer forms to include CompanyID instead of CompanyName for better consistency in customer management. - Enhanced Swagger documentation to accurately reflect the new structure and authorization details for customer-related endpoints.
This commit is contained in:
@@ -63,8 +63,8 @@ type Company struct {
|
||||
Currency string `bson:"currency" json:"currency"` // Default: "USD"
|
||||
Timezone string `bson:"timezone" json:"timezone"` // Default: "UTC"
|
||||
|
||||
// Customer assignment (for login credentials)
|
||||
CustomerID *string `bson:"customer_id,omitempty" json:"customer_id,omitempty"`
|
||||
// Company ownership - which customer owns this company
|
||||
OwnerCustomerID *string `bson:"owner_customer_id,omitempty" json:"owner_customer_id,omitempty"`
|
||||
|
||||
// Audit fields
|
||||
CreatedBy *string `bson:"created_by,omitempty" json:"created_by,omitempty"`
|
||||
@@ -165,7 +165,7 @@ type CompanyResponse struct {
|
||||
Language string `json:"language"`
|
||||
Currency string `json:"currency"`
|
||||
Timezone string `json:"timezone"`
|
||||
CustomerID *string `json:"customer_id,omitempty"`
|
||||
OwnerCustomerID *string `json:"owner_customer_id,omitempty"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
CreatedBy *string `json:"created_by,omitempty"`
|
||||
@@ -198,7 +198,7 @@ func (c *Company) ToResponse() *CompanyResponse {
|
||||
Language: c.Language,
|
||||
Currency: c.Currency,
|
||||
Timezone: c.Timezone,
|
||||
CustomerID: c.CustomerID,
|
||||
OwnerCustomerID: c.OwnerCustomerID,
|
||||
CreatedAt: c.CreatedAt,
|
||||
UpdatedAt: c.UpdatedAt,
|
||||
CreatedBy: c.CreatedBy,
|
||||
|
||||
@@ -61,9 +61,6 @@ type UpdateCompanyForm struct {
|
||||
// Tags for categorization and search
|
||||
Tags *CompanyTagsForm `json:"tags,omitempty"`
|
||||
|
||||
// Customer assignment (for login credentials)
|
||||
CustomerID *string `json:"customer_id,omitempty" valid:"optional,uuid"`
|
||||
|
||||
// Settings
|
||||
Language *string `json:"language,omitempty" valid:"optional,in(en|ar|fr|es|de|zh|ja|ko)"`
|
||||
Currency *string `json:"currency,omitempty" valid:"optional,in(USD|EUR|GBP|JPY|CAD|AUD|CHF|CNY|INR|BRL)"`
|
||||
|
||||
@@ -38,8 +38,6 @@ func (h *Handler) RegisterRoutes(e *echo.Echo) {
|
||||
adminV1.GET("/search", h.SearchCompanies)
|
||||
adminV1.PATCH("/:id/status", h.UpdateCompanyStatus)
|
||||
adminV1.PATCH("/:id/verification", h.UpdateCompanyVerification)
|
||||
adminV1.POST("/:id/assign-customer", h.AssignCustomer)
|
||||
adminV1.POST("/:id/unassign-customer", h.UnassignCustomer)
|
||||
adminV1.PUT("/:id/tags", h.UpdateCompanyTags)
|
||||
adminV1.POST("/:id/tags/add", h.AddTags)
|
||||
adminV1.POST("/:id/tags/remove", h.RemoveTags)
|
||||
@@ -370,86 +368,6 @@ func (h *Handler) UpdateCompanyVerification(c echo.Context) error {
|
||||
}, "Company verification updated successfully")
|
||||
}
|
||||
|
||||
// AssignCustomer assigns a customer to a company (Web Panel)
|
||||
// @Summary Assign customer to company
|
||||
// @Description Assign a customer to a company for login credentials
|
||||
// @Tags Companies-Admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Company ID"
|
||||
// @Param assignment body AssignCustomerForm true "Customer assignment information"
|
||||
// @Success 200 {object} response.APIResponse "Customer assigned successfully"
|
||||
// @Failure 400 {object} response.APIResponse "Bad request - Invalid input data"
|
||||
// @Failure 404 {object} response.APIResponse "Not found - Company not found"
|
||||
// @Failure 409 {object} response.APIResponse "Conflict - Customer already assigned"
|
||||
// @Failure 422 {object} response.APIResponse "Validation error - Invalid request data"
|
||||
// @Failure 500 {object} response.APIResponse "Internal server error"
|
||||
// @Security BearerAuth
|
||||
// @Router /admin/v1/companies/{id}/assign-customer [post]
|
||||
func (h *Handler) AssignCustomer(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
form, err := response.Parse[AssignCustomerForm](c)
|
||||
if err != nil {
|
||||
return response.ValidationError(c, "Invalid request data", err.Error())
|
||||
}
|
||||
|
||||
// Get user ID from JWT token context
|
||||
assignedBy, err := user.GetUserIDFromContext(c)
|
||||
if err != nil {
|
||||
return response.Unauthorized(c, "User not authenticated")
|
||||
}
|
||||
|
||||
err = h.service.AssignCustomer(c.Request().Context(), id, form, &assignedBy)
|
||||
if err != nil {
|
||||
if err.Error() == "company not found" {
|
||||
return response.NotFound(c, "Company not found")
|
||||
}
|
||||
if err.Error() == "customer is already assigned to another company" {
|
||||
return response.Conflict(c, err.Error())
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to assign customer")
|
||||
}
|
||||
|
||||
return response.Success(c, map[string]interface{}{
|
||||
"message": "Customer assigned successfully",
|
||||
}, "Customer assigned successfully")
|
||||
}
|
||||
|
||||
// UnassignCustomer unassigns a customer from a company (Web Panel)
|
||||
// @Summary Unassign customer from company
|
||||
// @Description Remove customer assignment from a company
|
||||
// @Tags Companies-Admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Company ID"
|
||||
// @Success 200 {object} response.APIResponse "Customer unassigned successfully"
|
||||
// @Failure 400 {object} response.APIResponse "Bad request - Invalid company ID"
|
||||
// @Failure 404 {object} response.APIResponse "Not found - Company not found"
|
||||
// @Failure 500 {object} response.APIResponse "Internal server error"
|
||||
// @Security BearerAuth
|
||||
// @Router /admin/v1/companies/{id}/unassign-customer [post]
|
||||
func (h *Handler) UnassignCustomer(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
|
||||
// Get user ID from JWT token context
|
||||
unassignedBy, err := user.GetUserIDFromContext(c)
|
||||
if err != nil {
|
||||
return response.Unauthorized(c, "User not authenticated")
|
||||
}
|
||||
|
||||
err = h.service.UnassignCustomer(c.Request().Context(), id, &unassignedBy)
|
||||
if err != nil {
|
||||
if err.Error() == "company not found" {
|
||||
return response.NotFound(c, "Company not found")
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to unassign customer")
|
||||
}
|
||||
|
||||
return response.Success(c, map[string]interface{}{
|
||||
"message": "Customer unassigned successfully",
|
||||
}, "Customer unassigned successfully")
|
||||
}
|
||||
|
||||
// UpdateCompanyTags updates company tags (Web Panel)
|
||||
// @Summary Update company tags
|
||||
// @Description Update company tags (CPV, categories, keywords, specializations, certifications)
|
||||
|
||||
@@ -30,8 +30,6 @@ type Repository interface {
|
||||
CountWithCustomer(ctx context.Context) (int64, error)
|
||||
UpdateStatus(ctx context.Context, id string, status CompanyStatus) error
|
||||
UpdateVerification(ctx context.Context, id string, isVerified, isCompliant bool, complianceNotes *string) error
|
||||
AssignCustomer(ctx context.Context, id string, customerID string) error
|
||||
UnassignCustomer(ctx context.Context, id string) error
|
||||
UpdateTags(ctx context.Context, id string, tags *CompanyTags) error
|
||||
AddTags(ctx context.Context, id string, cpvCodes []string, categories []string, keywords []string, specializations []string, certifications []string) error
|
||||
RemoveTags(ctx context.Context, id string, cpvCodes []string, categories []string, keywords []string, specializations []string, certifications []string) error
|
||||
@@ -591,66 +589,6 @@ func (r *companyRepository) UpdateVerification(ctx context.Context, id string, i
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssignCustomer assigns a customer to a company
|
||||
func (r *companyRepository) AssignCustomer(ctx context.Context, id string, customerID string) error {
|
||||
// Get company first
|
||||
company, err := r.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Assign customer
|
||||
company.CustomerID = &customerID
|
||||
company.SetUpdatedAt(time.Now().Unix())
|
||||
|
||||
// Update in database
|
||||
err = r.ormRepo.Update(ctx, company)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to assign customer to company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
"customer_id": customerID,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
r.logger.Info("Customer assigned to company successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"customer_id": customerID,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnassignCustomer unassigns a customer from a company
|
||||
func (r *companyRepository) UnassignCustomer(ctx context.Context, id string) error {
|
||||
// Get company first
|
||||
company, err := r.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unassign customer
|
||||
company.CustomerID = nil
|
||||
company.SetUpdatedAt(time.Now().Unix())
|
||||
|
||||
// Update in database
|
||||
err = r.ormRepo.Update(ctx, company)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to unassign customer from company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
r.logger.Info("Customer unassigned from company successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateTags updates company tags
|
||||
func (r *companyRepository) UpdateTags(ctx context.Context, id string, tags *CompanyTags) error {
|
||||
// Get company first
|
||||
|
||||
@@ -25,11 +25,6 @@ type Service interface {
|
||||
UpdateCompanyStatus(ctx context.Context, id string, form *UpdateCompanyStatusForm, updatedBy *string) error
|
||||
UpdateCompanyVerification(ctx context.Context, id string, form *UpdateCompanyVerificationForm, updatedBy *string) error
|
||||
|
||||
// Customer assignment
|
||||
AssignCustomer(ctx context.Context, id string, form *AssignCustomerForm, assignedBy *string) error
|
||||
UnassignCustomer(ctx context.Context, id string, unassignedBy *string) error
|
||||
GetCompanyByCustomerID(ctx context.Context, customerID string) (*Company, error)
|
||||
|
||||
// Tag management
|
||||
UpdateCompanyTags(ctx context.Context, id string, form *UpdateCompanyTagsForm, updatedBy *string) error
|
||||
AddTags(ctx context.Context, id string, form *AddTagsForm, updatedBy *string) error
|
||||
@@ -101,7 +96,6 @@ func (s *companyService) CreateCompany(ctx context.Context, form *CreateCompanyF
|
||||
Phone: form.Phone,
|
||||
Email: form.Email,
|
||||
Tags: s.convertCompanyTagsForm(form.Tags),
|
||||
CustomerID: form.CustomerID,
|
||||
IsVerified: false,
|
||||
IsCompliant: false,
|
||||
Language: s.getDefaultLanguage(form.Language),
|
||||
@@ -238,10 +232,6 @@ func (s *companyService) UpdateCompany(ctx context.Context, id string, form *Upd
|
||||
company.Tags = s.convertCompanyTagsForm(form.Tags)
|
||||
}
|
||||
|
||||
if form.CustomerID != nil {
|
||||
company.CustomerID = form.CustomerID
|
||||
}
|
||||
|
||||
if form.Language != nil {
|
||||
company.Language = *form.Language
|
||||
}
|
||||
@@ -479,66 +469,6 @@ func (s *companyService) UpdateCompanyVerification(ctx context.Context, id strin
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssignCustomer assigns a customer to a company
|
||||
func (s *companyService) AssignCustomer(ctx context.Context, id string, form *AssignCustomerForm, assignedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Check if customer is already assigned to another company
|
||||
existingCompany, _ := s.repository.GetByCustomerID(ctx, form.CustomerID)
|
||||
if existingCompany != nil {
|
||||
return errors.New("customer is already assigned to another company")
|
||||
}
|
||||
|
||||
// Assign customer
|
||||
err = s.repository.AssignCustomer(ctx, id, form.CustomerID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to assign customer to company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
"customer_id": form.CustomerID,
|
||||
})
|
||||
return errors.New("failed to assign customer to company")
|
||||
}
|
||||
|
||||
s.logger.Info("Customer assigned to company successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"customer_id": form.CustomerID,
|
||||
"assigned_by": assignedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnassignCustomer unassigns a customer from a company
|
||||
func (s *companyService) UnassignCustomer(ctx context.Context, id string, unassignedBy *string) error {
|
||||
// Get company to check if exists
|
||||
_, err := s.repository.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New("company not found")
|
||||
}
|
||||
|
||||
// Unassign customer
|
||||
err = s.repository.UnassignCustomer(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to unassign customer from company", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"company_id": id,
|
||||
})
|
||||
return errors.New("failed to unassign customer from company")
|
||||
}
|
||||
|
||||
s.logger.Info("Customer unassigned from company successfully", map[string]interface{}{
|
||||
"company_id": id,
|
||||
"unassigned_by": unassignedBy,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCompanyByCustomerID retrieves a company by customer ID
|
||||
func (s *companyService) GetCompanyByCustomerID(ctx context.Context, customerID string) (*Company, error) {
|
||||
company, err := s.repository.GetByCustomerID(ctx, customerID)
|
||||
|
||||
Reference in New Issue
Block a user