Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions entity-service/internal/domain/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,71 @@ type SearchCaseCommentsResponse struct {
HasMore bool `json:"hasMore"`
}

// ActivityType classifies an entry in a case activity feed.
type ActivityType string

const (
// ActivityTypeComment is a comment or work note on the case.
ActivityTypeComment ActivityType = "comment"
// ActivityTypeAttachment is a file attached to the case.
ActivityTypeAttachment ActivityType = "attachment"
// ActivityTypeFieldChange is a recorded change to one or more case fields.
ActivityTypeFieldChange ActivityType = "field_change"
)

// FieldChange describes a single field mutation recorded on a case.
type FieldChange struct {
Field string `json:"field"`
FieldLabel string `json:"fieldLabel"`
PreviousValue string `json:"previousValue"`
NewValue string `json:"newValue"`
}

// CaseActivity is a single entry in a case activity feed. It is a discriminated
// union on Type: the shared fields are always present, while the type-specific
// fields are populated only for the matching Type (CommentType for comments;
// FileName/ContentType/SizeBytes/DownloadURL for attachments; Changes for field
// changes).
type CaseActivity struct {
ID string `json:"id"`
Type ActivityType `json:"type"`
Content string `json:"content"`
CreatedOn time.Time `json:"createdOn"`
CreatedBy string `json:"createdBy"`
CreatedByFirstName string `json:"createdByFirstName"`
CreatedByLastName string `json:"createdByLastName"`
CreatedByFullName string `json:"createdByFullName"`
// CommentType is set only for Type == ActivityTypeComment.
CommentType *CommentType `json:"commentType,omitempty"`
// FileName, ContentType, SizeBytes, and DownloadURL are set only for
// Type == ActivityTypeAttachment.
FileName string `json:"fileName,omitempty"`
ContentType string `json:"contentType,omitempty"`
SizeBytes int `json:"sizeBytes,omitempty"`
DownloadURL string `json:"downloadUrl,omitempty"`
// Changes is set only for Type == ActivityTypeFieldChange.
Changes []FieldChange `json:"changes,omitempty"`
}

// SearchCaseActivitiesRequest is the input for listing the activity feed of a case.
// CaseID is populated from the URL path parameter and is not part of the JSON body.
// When IncludeFieldChanges is nil or false, only comment and attachment entries are returned.
type SearchCaseActivitiesRequest struct {
CaseID string `json:"-"`
Pagination Pagination `json:"pagination"`
IncludeFieldChanges *bool `json:"includeFieldChanges,omitempty"`
}

// SearchCaseActivitiesResponse is the paginated result of a case activity search.
// HasMore is true when additional pages are available beyond the current offset.
type SearchCaseActivitiesResponse struct {
Activity []CaseActivity `json:"activity"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
HasMore bool `json:"hasMore"`
}

// Comment is a generic comment associated with any reference entity type
// (case, conversation, change_request, etc.).
type Comment struct {
Expand Down
16 changes: 16 additions & 0 deletions entity-service/internal/handler/case_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ func (h *CaseHandler) SearchCaseComments(w http.ResponseWriter, r *http.Request)
_ = json.NewEncoder(w).Encode(resp)
}

// SearchCaseActivities handles POST /cases/{id}/activities/search.
func (h *CaseHandler) SearchCaseActivities(w http.ResponseWriter, r *http.Request) {
var req domain.SearchCaseActivitiesRequest
if !decodeRequest(w, r, &req) {
return
}
req.CaseID = r.PathValue("id")
resp, err := h.svc.SearchCaseActivities(r.Context(), req)
if err != nil {
writeServiceError(w, r, err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}

// SearchCases handles POST /cases/search.
func (h *CaseHandler) SearchCases(w http.ResponseWriter, r *http.Request) {
var req domain.SearchCasesRequest
Expand Down
1 change: 1 addition & 0 deletions entity-service/internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func NewRouter(db *pgxpool.Pool, cfg *config.Config) http.Handler {
mux.HandleFunc("POST /cases", caseHandler.CreateCase)
mux.HandleFunc("POST /cases/search", caseHandler.SearchCases)
mux.HandleFunc("POST /cases/{id}/comments", caseHandler.CreateCaseComment)
mux.HandleFunc("POST /cases/{id}/activities/search", caseHandler.SearchCaseActivities)
mux.HandleFunc("POST /attachments", caseHandler.CreateCaseAttachment)
mux.HandleFunc("POST /attachments/search", caseHandler.SearchCaseAttachments)
mux.HandleFunc("GET /attachments/{id}/content", caseHandler.GetCaseAttachmentContent)
Expand Down
4 changes: 4 additions & 0 deletions entity-service/internal/service/case_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ func (s *caseService) SearchCaseAttachments(_ context.Context, _ domain.SearchAt
return domain.SearchAttachmentsResponse{}, &apierror.ServiceUnavailableError{Msg: "attachments are only supported for the ServiceNow data source"}
}

func (s *caseService) SearchCaseActivities(_ context.Context, _ domain.SearchCaseActivitiesRequest) (domain.SearchCaseActivitiesResponse, error) {
return domain.SearchCaseActivitiesResponse{}, &apierror.ServiceUnavailableError{Msg: "case activities are only supported for the ServiceNow data source"}
}

func (s *caseService) GetCaseAttachmentContent(_ context.Context, _ string) ([]byte, string, error) {
return nil, "", &apierror.ServiceUnavailableError{Msg: "attachments are only supported for the ServiceNow data source"}
}
Expand Down
5 changes: 5 additions & 0 deletions entity-service/internal/service/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ type CaseService interface {
// SearchCaseAttachments returns a paginated list of attachments for the case identified
// by req.CaseID. A ValidationError is returned for invalid input.
SearchCaseAttachments(ctx context.Context, req domain.SearchAttachmentsRequest) (domain.SearchAttachmentsResponse, error)
// SearchCaseActivities returns a paginated activity feed (comments, attachments, and
// optionally field changes) for the case identified by req.CaseID. Field-change entries
// are included only when req.IncludeFieldChanges is set. A ValidationError is returned
// for invalid input. Supported by the ServiceNow data source only.
SearchCaseActivities(ctx context.Context, req domain.SearchCaseActivitiesRequest) (domain.SearchCaseActivitiesResponse, error)
// GetCaseAttachmentContent returns the raw binary content and its Content-Type
// for the attachment identified by attachmentID.
// A NotFoundError is returned if absent.
Expand Down
125 changes: 125 additions & 0 deletions entity-service/internal/service/sn_case_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,131 @@ func (s *snCaseService) SearchCaseAttachments(ctx context.Context, req domain.Se
}, nil
}

type snSearchActivitiesPayload struct {
Pagination snProjectPagination `json:"pagination"`
IncludeFieldChanges *bool `json:"includeFieldChanges,omitempty"`
}

type snFieldChange struct {
Field string `json:"field"`
FieldLabel string `json:"fieldLabel"`
PreviousValue string `json:"previousValue"`
NewValue string `json:"newValue"`
}

type snActivity struct {
ID string `json:"id"`
Type string `json:"type"`
Content string `json:"content"`
CreatedOn string `json:"createdOn"`
CreatedBy string `json:"createdBy"`
CreatedByFirstName string `json:"createdByFirstName"`
CreatedByLastName string `json:"createdByLastName"`
CreatedByFullName string `json:"createdByFullName"`
CommentType string `json:"commentType"`
FileName string `json:"fileName"`
ContentType string `json:"contentType"`
SizeBytes int `json:"sizeBytes"`
DownloadURL string `json:"downloadUrl"`
Changes []snFieldChange `json:"changes"`
}

type snSearchActivitiesResponse struct {
Activity []snActivity `json:"activity"`
Offset int `json:"offset"`
Limit int `json:"limit"`
TotalRecords int `json:"totalRecords"`
}

func (s *snCaseService) SearchCaseActivities(ctx context.Context, req domain.SearchCaseActivitiesRequest) (domain.SearchCaseActivitiesResponse, error) {
if err := normalizePagination(&req.Pagination); err != nil {
return domain.SearchCaseActivitiesResponse{}, err
}

token := middleware.UserIDTokenFromContext(ctx)
if token == "" {
return domain.SearchCaseActivitiesResponse{}, &apierror.UnauthorizedError{Msg: "x-user-id-token header is required"}
}

if err := validateUUIDs("id", []string{req.CaseID}); err != nil {
return domain.SearchCaseActivitiesResponse{}, err
}

payload := snSearchActivitiesPayload{
Pagination: snProjectPagination{Limit: req.Pagination.Limit, Offset: req.Pagination.Offset},
IncludeFieldChanges: req.IncludeFieldChanges,
}

raw, err := s.client.Post(ctx, "/cases/"+uuidToSysid(req.CaseID)+"/activities/search", token, payload)
if err != nil {
return domain.SearchCaseActivitiesResponse{}, err
}

var snResp snSearchActivitiesResponse
if err := json.Unmarshal(raw, &snResp); err != nil {
return domain.SearchCaseActivitiesResponse{}, fmt.Errorf("sn search activities: parse response: %w", err)
}

activities := make([]domain.CaseActivity, 0, len(snResp.Activity))
for _, a := range snResp.Activity {
createdOn, err := time.Parse(snCreatedOnLayout, a.CreatedOn)
if err != nil {
return domain.SearchCaseActivitiesResponse{}, fmt.Errorf("sn search activities: parse createdOn %q: %w", a.CreatedOn, err)
}
activity := domain.CaseActivity{
ID: sysidToUUID(a.ID),
Type: domain.ActivityType(a.Type),
Content: a.Content,
CreatedOn: createdOn,
CreatedBy: a.CreatedBy,
CreatedByFirstName: a.CreatedByFirstName,
CreatedByLastName: a.CreatedByLastName,
CreatedByFullName: a.CreatedByFullName,
}
switch domain.ActivityType(a.Type) {
case domain.ActivityTypeComment:
var ct domain.CommentType
switch a.CommentType {
case "comments", "comment":
ct = domain.CommentTypeComment
case "work_notes", "work_note":
ct = domain.CommentTypeWorkNote
case "activity":
ct = domain.CommentTypeActivity
default:
ct = domain.CommentTypeComment
}
activity.CommentType = &ct
case domain.ActivityTypeAttachment:
activity.FileName = a.FileName
activity.ContentType = a.ContentType
activity.SizeBytes = a.SizeBytes
activity.DownloadURL = a.DownloadURL
case domain.ActivityTypeFieldChange:
changes := make([]domain.FieldChange, 0, len(a.Changes))
for _, ch := range a.Changes {
changes = append(changes, domain.FieldChange{
Field: ch.Field,
FieldLabel: ch.FieldLabel,
PreviousValue: ch.PreviousValue,
NewValue: ch.NewValue,
})
}
activity.Changes = changes
}
activities = append(activities, activity)
}

total := snResp.TotalRecords
return domain.SearchCaseActivitiesResponse{
Activity: activities,
Total: total,
Limit: req.Pagination.Limit,
Offset: req.Pagination.Offset,
HasMore: req.Pagination.Offset+len(activities) < total,
}, nil
}

func (s *snCaseService) GetCaseAttachmentContent(ctx context.Context, attachmentID string) ([]byte, string, error) {
token := middleware.UserIDTokenFromContext(ctx)
if token == "" {
Expand Down
Loading