diff --git a/external/router.go b/external/router.go index 6d93223..b5d8080 100644 --- a/external/router.go +++ b/external/router.go @@ -27,7 +27,8 @@ func Router(r chi.Router, cfg *config.Config) { v3.Router(r, cfg) r.Get("/swagger/*", httpSwagger.Handler( - httpSwagger.URL("https://api.vatusa.dev/v3/swagger/doc.json"), + //httpSwagger.URL("https://api.vatusa.dev/swagger/doc.json"), + httpSwagger.URL("http://localhost:3000/swagger/swagger.json"), )) } diff --git a/external/v3/disciplinary-log/disciplinary_log.go b/external/v3/disciplinary-log/disciplinary_log.go index 0a91069..1b0580b 100644 --- a/external/v3/disciplinary-log/disciplinary_log.go +++ b/external/v3/disciplinary-log/disciplinary_log.go @@ -11,7 +11,7 @@ import ( ) type Request struct { - Entry string `json:"entry" example:"Changed Preferred OIs to RP" validate:"required"` + Entry string `json:"entry" example:"Misconduct in discord" validate:"required"` VATUSAOnly bool `json:"vatusa_only" example:"true"` } diff --git a/external/v3/user/login.go b/external/v3/user/login.go index 435632b..604495d 100644 --- a/external/v3/user/login.go +++ b/external/v3/user/login.go @@ -10,6 +10,7 @@ import ( "github.com/VATUSA/primary-api/pkg/database/models" "github.com/VATUSA/primary-api/pkg/oauth" "github.com/VATUSA/primary-api/pkg/utils" + vatsim_api "github.com/VATUSA/primary-api/pkg/vatsim/api" gonanoid "github.com/matoous/go-nanoid" "golang.org/x/oauth2" "io" @@ -20,40 +21,6 @@ import ( "time" ) -type VATSIMUser struct { - CID string `json:"cid"` - Personal struct { - FirstName string `json:"name_first"` - LastName string `json:"name_last"` - FullName string `json:"name_full"` - Email string `json:"email"` - } `json:"personal"` - VATSIM struct { - ControllerRating struct { - ID int `json:"id"` - Short string `json:"short"` - Long string `json:"long"` - } `json:"rating"` - PilotRating struct { - ID int `json:"id"` - Short string `json:"short"` - Long string `json:"long"` - } `json:"pilotrating"` - Region struct { - ID string `json:"id"` - Name string `json:"name"` - } `json:"region"` - Division struct { - ID string `json:"id"` - Name string `json:"name"` - } `json:"division"` - Subdivision struct { - ID string `json:"id"` - Name string `json:"name"` - } `json:"subdivision"` - } -} - func GetLogin(w http.ResponseWriter, r *http.Request) { state, err := gonanoid.Generate("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 64) if err != nil { @@ -119,7 +86,7 @@ func GetLoginCallback(w http.ResponseWriter, r *http.Request) { res, err := http.NewRequest("GET", fmt.Sprintf("%s%s", config.Cfg.OAuth.BaseURL, config.Cfg.OAuth.UserInfoURL), nil) res.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken)) res.Header.Add("Accept", "application/json") - res.Header.Add("User-Agent", "usa-primary-api") + res.Header.Add("User-Agent", "vatusa-primary-api") if err != nil { utils.Render(w, r, utils.ErrInternalServerWithErr(err)) return @@ -149,7 +116,7 @@ func GetLoginCallback(w http.ResponseWriter, r *http.Request) { return } - user := &VATSIMUser{} + user := &vatsim_api.User{} if err := json.Unmarshal(body, user); err != nil { utils.Render(w, r, utils.ErrInternalServerWithErr(err)) return diff --git a/pkg/constants/facility_tiers.go b/pkg/constants/facility_tiers.go new file mode 100644 index 0000000..4f9fd34 --- /dev/null +++ b/pkg/constants/facility_tiers.go @@ -0,0 +1,63 @@ +package constants + +var ( + FacilityTierOne = map[FacilityID][]FacilityID{ + AlbuquerqueFacility: { + LosAngelesFacility, + DenverFacility, + KansasCityFacility, + FortWorthFacility, + HoustonFacility, + }, + LosAngelesFacility: { + OaklandFacility, + SaltLakeFacility, + DenverFacility, + AlbuquerqueFacility, + HonoluluFacility, + }, + OaklandFacility: { + SeattleFacility, + SaltLakeFacility, + LosAngelesFacility, + HonoluluFacility, + }, + SeattleFacility: { + AnchorageFacility, + SaltLakeFacility, + OaklandFacility, + HonoluluFacility, + }, + HonoluluFacility: { + AnchorageFacility, + SeattleFacility, + OaklandFacility, + LosAngelesFacility, + }, + DenverFacility: { + SaltLakeFacility, + MinneapolisFacility, + KansasCityFacility, + AlbuquerqueFacility, + LosAngelesFacility, + }, + HoustonFacility: { + AlbuquerqueFacility, + FortWorthFacility, + MemphisFacility, + JacksonvilleFacility, + }, + + //TODO - finish me + } +) + +func (a FacilityID) IsTierOne(b FacilityID) bool { + for _, fac := range FacilityTierOne[a] { + if fac == b { + return true + } + } + + return false +} diff --git a/pkg/vatsim/api/user.go b/pkg/vatsim/api/user.go new file mode 100644 index 0000000..56c1837 --- /dev/null +++ b/pkg/vatsim/api/user.go @@ -0,0 +1,35 @@ +package vatsim_api + +type User struct { + CID string `json:"cid"` + Personal struct { + FirstName string `json:"name_first"` + LastName string `json:"name_last"` + FullName string `json:"name_full"` + Email string `json:"email"` + } `json:"personal"` + VATSIM struct { + ControllerRating struct { + ID int `json:"id"` + Short string `json:"short"` + Long string `json:"long"` + } `json:"rating"` + PilotRating struct { + ID int `json:"id"` + Short string `json:"short"` + Long string `json:"long"` + } `json:"pilotrating"` + Region struct { + ID string `json:"id"` + Name string `json:"name"` + } `json:"region"` + Division struct { + ID string `json:"id"` + Name string `json:"name"` + } `json:"division"` + Subdivision struct { + ID string `json:"id"` + Name string `json:"name"` + } `json:"subdivision"` + } +} diff --git a/pkg/vatsim/webhooks/user.go b/pkg/vatsim/webhooks/user.go new file mode 100644 index 0000000..c8ee75f --- /dev/null +++ b/pkg/vatsim/webhooks/user.go @@ -0,0 +1,63 @@ +package vatsim_webhooks + +import ( + "encoding/json" + "time" +) + +type Message struct { + CID uint `json:"resource"` + Actions []Action `json:"actions"` +} + +type WebhookActionType string + +const ( + UserCreation WebhookActionType = "member_created_action" + UserChanged WebhookActionType = "member_changed_action" +) + +type Action struct { + Type WebhookActionType `json:"action"` + Authority string `json:"authority"` + Comment string `json:"comment"` + Deltas []Delta `json:"deltas"` + Timestamp time.Time `json:"timestamp"` +} + +type Delta struct { + Field string `json:"field"` + Before string `json:"before"` + After string `json:"after"` +} + +// TODO - does user creation webhook only send to VATUSA if they select division_id = USA? +// TODO - Rating changes? if VATUSA is the authority its not gonna send the webhook back to VATUSA correct? or it will? +func ConsumeMessage(rawMessage string) error { + msg := Message{} + if err := json.Unmarshal([]byte(rawMessage), &msg); err != nil { + return err + } + + for _, action := range msg.Actions { + switch action.Type { + case UserCreation: + // + case UserChanged: + // + default: + // TODO - log unknown actionType + + } + } + + return nil +} + +func HandleUserCreation(action Action) error { + return nil +} + +func HandleUserChange(action Action) error { + return nil +}