Skip to content

Commit

Permalink
feat(next/api): include "fields" in ticket list response
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Apr 7, 2024
1 parent fe55aad commit 033acbf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions next/api/src/model/Ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Organization } from './Organization';
import { Reply, TinyReplyInfo } from './Reply';
import { TinyUserInfo, User, systemUser } from './User';
import { Watch } from './Watch';
import { TicketFieldValue } from './TicketFieldValue';

export class Status {
// 0~99 未开始处理
Expand Down Expand Up @@ -228,6 +229,9 @@ export class Ticket extends Model {
@field()
closedAt?: Date;

@hasOneThroughPointer(() => TicketFieldValue)
fieldValue?: TicketFieldValue;

getUrlForEndUser() {
return `${config.host}/tickets/${this.nid}`;
}
Expand Down
8 changes: 8 additions & 0 deletions next/api/src/response/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export interface TicketResponseOptions {
class BaseTicketResponse {
constructor(readonly ticket: Ticket, private transferFile = false) {}

encodeFields() {
return this.ticket.fieldValue?.values.map((v) => ({
id: v.field,
value: v.value,
}));
}

toJSON({ includeMetaKeys = [], includePrivateTags, includeTags }: TicketResponseOptions = {}) {
return {
id: this.ticket.id,
Expand All @@ -40,6 +47,7 @@ class BaseTicketResponse {
language: this.ticket.language,
tags: includeTags ? this.ticket.tags : undefined,
privateTags: includePrivateTags ? this.ticket.privateTags : undefined,
fields: this.encodeFields(),
createdAt: this.ticket.createdAt.toISOString(),
updatedAt: this.ticket.updatedAt.toISOString(),
};
Expand Down
6 changes: 6 additions & 0 deletions next/api/src/router/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const includeSchema = yup.object({
includeCategoryPath: yup.bool(),
includeUnreadCount: yup.bool(),
includeTag: yup.bool(),
includeFields: yup.bool(),
});

export const ticketFiltersSchema = yup.object({
Expand Down Expand Up @@ -262,6 +263,11 @@ router.get(
},
});
}
if (params.includeFields) {
finalQuery.preload('fieldValue', {
authOptions: { useMasterKey: true },
});
}

let tickets: Ticket[];
if (params.count && !count) {
Expand Down

0 comments on commit 033acbf

Please sign in to comment.