57 lines
993 B
Go
Raw Normal View History

2023-10-08 15:59:42 +07:00
package mycontext
import (
"context"
"furtuna-be/internal/constants/role"
"furtuna-be/internal/entity"
)
type ContextKey string
type Context interface {
context.Context
RequestedBy() int64
IsSuperAdmin() bool
2024-06-03 14:40:50 +07:00
GetPartnerID() *int64
2023-10-08 15:59:42 +07:00
}
type MyContextImpl struct {
context.Context
requestedBy int64
requestID string
2024-06-03 14:40:50 +07:00
partnerID int64
2023-10-08 15:59:42 +07:00
roleID int
}
func (m *MyContextImpl) RequestedBy() int64 {
return m.requestedBy
}
func (m *MyContextImpl) IsSuperAdmin() bool {
return m.roleID == int(role.SuperAdmin)
}
2024-06-03 14:40:50 +07:00
func (m *MyContextImpl) GetPartnerID() *int64 {
if m.partnerID != 0 {
return &m.partnerID
}
return nil
}
2023-10-08 15:59:42 +07:00
func NewMyContext(parent context.Context, claims *entity.JWTAuthClaims) (*MyContextImpl, error) {
return &MyContextImpl{
Context: parent,
requestedBy: claims.UserID,
2024-06-03 14:40:50 +07:00
partnerID: claims.PartnerID,
2023-10-08 15:59:42 +07:00
roleID: claims.Role,
}, nil
}
func NewContext(parent context.Context) *MyContextImpl {
return &MyContextImpl{
Context: parent,
}
}