Files
test_deploy/internal/http/company_distributor.go
Alex Shevchuk 61fc0d2747
Some checks failed
Deploy Production / Deploy to Staging (push) Has been skipped
Go Linter / Run golangci-lint (api_gateway) (push) Failing after 2m31s
Go Linter / Build golang services (api_gateway) (push) Has been skipped
Go Linter / Tag Commit (push) Has been skipped
Go Linter / Push Docker Images (api_gateway) (push) Has been skipped
71
2025-09-17 14:32:06 +03:00

269 lines
9.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package http_router
import (
"encoding/json"
"log/slog"
"net/http"
"git-molva.ru/Molva/molva-backend/services/api_gateway/internal/constants"
rmodel "git-molva.ru/Molva/molva-backend/services/api_gateway/internal/request_model"
"github.com/gorilla/mux"
)
// @Summary Получить список компаний дистрибьютора
// @Description Получение списка всех компаний, принадлежащих дистрибьютору
// @Tags distributors
// @Accept json
// @Produce json
// @Param distributor_id path string true "ID дистрибьютора"
// @Success 200 {object} rmodel.CompanyListGetResponse "Список компаний"
// @Failure 400 {object} map[string]string "Неверные данные запроса"
// @Failure 401 {object} map[string]string "Неавторизованный доступ"
// @Failure 500 {object} map[string]string "Внутренняя ошибка сервера"
// @Security BearerAuth
// @Router /api/v1/distributor/{distributor_id}/companies [get]
func (h *handler) getCompanyListDistributorHandler(w http.ResponseWriter, r *http.Request) {
const handlerName = "getCompanyListDistributorHandler"
var (
vars = mux.Vars(r)
distId = vars["distributor_id"]
)
result, err := h.distributorService.GetCompanyList(r.Context(), &rmodel.CompanyListGetRequest{
Id: distId,
})
if err != nil {
h.handleDistributorError(w, err)
h.logger.Error("error getting company list",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError)
h.logger.Error("error encoding response",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
}
}
// @Summary Получить компанию дистрибьютора по ID
// @Description Получение детальной информации о компании дистрибьютора
// @Tags distributors
// @Accept json
// @Produce json
// @Param distributor_id path string true "ID дистрибьютора"
// @Param company_id path string true "ID компании"
// @Success 200 {object} rmodel.CompanyByIdGetResponse "Информация о компании"
// @Failure 400 {object} map[string]string "Неверные данные запроса"
// @Failure 401 {object} map[string]string "Неавторизованный доступ"
// @Failure 404 {object} map[string]string "Компания не найдена"
// @Failure 500 {object} map[string]string "Внутренняя ошибка сервера"
// @Security BearerAuth
// @Router /api/v1/distributor/{distributor_id}/company/{company_id} [get]
func (h *handler) getCompanyByIdDistributorHandler(w http.ResponseWriter, r *http.Request) {
const handlerName = "getCompanyByIdDistributorHandler"
var (
vars = mux.Vars(r)
distId = vars["distributor_id"]
companyId = vars["company_id"]
)
result, err := h.distributorService.GetCompanyInfoById(r.Context(), &rmodel.CompanyByIdGetRequest{
UserId: distId,
CompanyId: companyId,
})
if err != nil {
h.handleDistributorError(w, err)
h.logger.Error("error getting company list",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError)
h.logger.Error("error encoding response",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
}
}
// @Summary Создать компанию дистрибьютора
// @Description Создание новой компании для дистрибьютора
// @Tags distributors
// @Accept json
// @Produce json
// @Param distributor_id path string true "ID дистрибьютора"
// @Param request body rmodel.CompanyCreateRequest true "Данные для создания компании"
// @Success 201 {object} rmodel.CompanyCreateResponse "Компания создана"
// @Failure 400 {object} map[string]string "Неверные данные запроса"
// @Failure 401 {object} map[string]string "Неавторизованный доступ"
// @Failure 500 {object} map[string]string "Внутренняя ошибка сервера"
// @Security BearerAuth
// @Router /api/v1/distributor/{distributor_id}/company [post]
func (h *handler) createCompanyDistributorHandler(w http.ResponseWriter, r *http.Request) {
const handlerName = "createCompanyDistributorHandler"
var (
vars = mux.Vars(r)
distId = vars["distributor_id"]
)
var request rmodel.CompanyCreateRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest)
h.logger.Error("error unmarshalling request: ",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
return
}
request.OwnerId = distId
result, err := h.distributorService.CreateCompany(r.Context(), &request)
if err != nil {
h.handleDistributorError(w, err)
h.logger.Error("error creating company",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
return
}
// TODO:
// h.createCreateCompanyFeedEvent(r.Context(), distId, false, resp, handlerName)
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(result); err != nil {
http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError)
h.logger.Error("error encoding response",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
}
}
// @Summary Обновить компанию дистрибьютора
// @Description Обновление информации о компании дистрибьютора
// @Tags distributors
// @Accept json
// @Produce json
// @Param distributor_id path string true "ID дистрибьютора"
// @Param company_id path string true "ID компании"
// @Param request body rmodel.CompanyUpdateRequest true "Данные для обновления"
// @Success 204 "Компания обновлена"
// @Failure 400 {object} map[string]string "Неверные данные запроса"
// @Failure 401 {object} map[string]string "Неавторизованный доступ"
// @Failure 404 {object} map[string]string "Компания не найдена"
// @Failure 500 {object} map[string]string "Внутренняя ошибка сервера"
// @Security BearerAuth
// @Router /api/v1/distributor/{distributor_id}/company/{company_id} [patch]
func (h *handler) updateCompanyDistributorHandler(w http.ResponseWriter, r *http.Request) {
const handlerName = "updateCompanyDistributorHandler"
var (
vars = mux.Vars(r)
companyId = vars["company_id"]
)
var request rmodel.CompanyUpdateRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest)
h.logger.Error("error unmarshalling request: ",
slog.String("error", err.Error()),
slog.String("handler", handlerName))
return
}
request.Id = companyId
if _, err := h.distributorService.UpdateCompanyInfo(r.Context(), &request); err != nil {
h.handleDistributorError(w, err)
h.logger.Error("error updating company info",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
return
}
// BUG: holds request for infinite time
// h.createEditCompanyFeedEvent(r.Context(), distId, false, comId, handlerName)
w.WriteHeader(http.StatusNoContent)
}
// @Summary Добавить участника в компанию
// @Description Добавление нового участника в компанию дистрибьютора
// @Tags distributors
// @Accept json
// @Produce json
// @Param distributor_id path string true "ID дистрибьютора"
// @Param company_id path string true "ID компании"
// @Param request body rmodel.AddDistributorCompanyMemberRequest true "Данные участника"
// @Success 201 "Участник добавлен"
// @Failure 400 {object} map[string]string "Неверные данные запроса"
// @Failure 401 {object} map[string]string "Неавторизованный доступ"
// @Failure 404 {object} map[string]string "Компания не найдена"
// @Failure 500 {object} map[string]string "Внутренняя ошибка сервера"
// @Security BearerAuth
// @Router /api/v1/distributor/{distributor_id}/company/{company_id} [post]
func (h *handler) addCompanyMemberDistributorHandler(w http.ResponseWriter, r *http.Request) {
const handlerName = "addCompanyMemberDistributorHandler"
var (
vars = mux.Vars(r)
companyId = vars["company_id"]
)
var request rmodel.AddDistributorCompanyMemberRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, constants.ErrBadRequest.Error(), http.StatusBadRequest)
h.logger.Error("error while unmarshalling request: ",
slog.String("error", err.Error()),
slog.String("handler", handlerName))
return
}
request.CompanyId = companyId
if _, err := h.distributorService.AddEmployee(r.Context(), &request); err != nil {
h.handleDistributorError(w, err)
h.logger.Error("error adding new staff member",
slog.String("error", err.Error()),
slog.String("handler", handlerName),
)
return
}
// TODO:
// h.createAddNewStaffMemberFeedEvent(r.Context(), distId, true, *req.NewStaffMember, companyId, handlerName)
w.WriteHeader(http.StatusNoContent)
}