Skip to content

Commit 4c80cae

Browse files
authored
approval: 承認待ち件数取得・承認完了書類非表示設定サブコマンドを追加 (#26)
- `xp approval wait` (GET /api/v1/approvals/wait) を追加 - `xp approval hidden` (PUT /api/v1/approvals/hidden) を追加 - 対応する schema JSON (approval.wait.json / approval.hidden.json) を同梱
1 parent 00264b7 commit 4c80cae

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

cmd/approval.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ var (
2222
approvalListShowHiddenDoc bool
2323
approvalListOutput string
2424
approvalListJQ string
25+
26+
approvalWaitFGID int
27+
approvalWaitFID int
28+
approvalWaitStep int
29+
approvalWaitOutput string
30+
approvalWaitJQ string
31+
32+
approvalHiddenShow bool
33+
approvalHiddenProxyUser string
34+
approvalHiddenOutput string
35+
approvalHiddenJQ string
2536
)
2637

2738
var approvalCmd = &cobra.Command{
@@ -36,6 +47,24 @@ var approvalListCmd = &cobra.Command{
3647
RunE: runApprovalList,
3748
}
3849

50+
var approvalWaitCmd = &cobra.Command{
51+
Use: "wait",
52+
Short: "Get approval counts and latest pending documents",
53+
Long: "Fetch approval counts and the latest pending documents via GET /api/v1/approvals/wait.",
54+
RunE: runApprovalWait,
55+
}
56+
57+
var approvalHiddenCmd = &cobra.Command{
58+
Use: "hidden <docid> [<docid>...]",
59+
Short: "Hide or show completed approval documents",
60+
Long: `Set the hidden flag on completed approval documents via PUT /api/v1/approvals/hidden.
61+
62+
By default the given document IDs are hidden. Pass --show to restore them.
63+
Only documents in the "承認完了" (completed) status may be specified.`,
64+
Args: cobra.MinimumNArgs(1),
65+
RunE: runApprovalHidden,
66+
}
67+
3968
func init() {
4069
rootCmd.AddCommand(approvalCmd)
4170
approvalCmd.AddCommand(approvalListCmd)
@@ -51,6 +80,21 @@ func init() {
5180
f.BoolVar(&approvalListShowHiddenDoc, "show-hidden-doc", false, "include hidden completed documents")
5281
f.StringVarP(&approvalListOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)")
5382
f.StringVar(&approvalListJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)")
83+
84+
approvalCmd.AddCommand(approvalWaitCmd)
85+
fw := approvalWaitCmd.Flags()
86+
fw.IntVar(&approvalWaitFGID, "fgid", 0, "form group ID (0 = omit)")
87+
fw.IntVar(&approvalWaitFID, "fid", 0, "form ID (0 = omit)")
88+
fw.IntVar(&approvalWaitStep, "step", 0, "approval step (0 = omit)")
89+
fw.StringVarP(&approvalWaitOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)")
90+
fw.StringVar(&approvalWaitJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)")
91+
92+
approvalCmd.AddCommand(approvalHiddenCmd)
93+
fh := approvalHiddenCmd.Flags()
94+
fh.BoolVar(&approvalHiddenShow, "show", false, "show (un-hide) the given documents instead of hiding them")
95+
fh.StringVar(&approvalHiddenProxyUser, "proxy-user", "", "proxy approver user code (required when toggling proxy-approved documents)")
96+
fh.StringVarP(&approvalHiddenOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)")
97+
fh.StringVar(&approvalHiddenJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)")
5498
}
5599

56100
func runApprovalList(cmd *cobra.Command, args []string) error {
@@ -108,3 +152,88 @@ func runApprovalList(cmd *cobra.Command, args []string) error {
108152
return nil
109153
})
110154
}
155+
156+
func runApprovalWait(cmd *cobra.Command, args []string) error {
157+
client, err := newClientFromFlags(cmd.Context())
158+
if err != nil {
159+
return err
160+
}
161+
162+
var params xpoint.ApprovalsWaitParams
163+
if approvalWaitFGID != 0 {
164+
v := approvalWaitFGID
165+
params.FormGroupID = &v
166+
}
167+
if approvalWaitFID != 0 {
168+
v := approvalWaitFID
169+
params.FormID = &v
170+
}
171+
if approvalWaitStep != 0 {
172+
v := approvalWaitStep
173+
params.Step = &v
174+
}
175+
176+
res, err := client.GetApprovalsWait(cmd.Context(), params)
177+
if err != nil {
178+
return err
179+
}
180+
181+
return render(res, resolveOutputFormat(approvalWaitOutput), approvalWaitJQ, func() error {
182+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
183+
fmt.Fprintln(w, "TYPE\tNAME\tCOUNT")
184+
for _, s := range res.StatusList {
185+
fmt.Fprintf(w, "%d\t%s\t%d\n", s.Type, s.Name, s.Count)
186+
}
187+
w.Flush()
188+
189+
if len(res.WaitList) > 0 {
190+
fmt.Fprintln(os.Stdout)
191+
fmt.Fprintln(os.Stdout, "最新の承認待ち書類:")
192+
w2 := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
193+
fmt.Fprintln(w2, "DOCID\tFORM_NAME\tWRITER\tWRITE_DATE\tTITLE")
194+
for _, d := range res.WaitList {
195+
fmt.Fprintf(w2, "%d\t%s\t%s\t%s\t%s\n", d.DocID, d.Name, d.WriterName, d.WriteDate, d.Title)
196+
}
197+
w2.Flush()
198+
}
199+
return nil
200+
})
201+
}
202+
203+
func runApprovalHidden(cmd *cobra.Command, args []string) error {
204+
client, err := newClientFromFlags(cmd.Context())
205+
if err != nil {
206+
return err
207+
}
208+
209+
docIDs := make([]int, 0, len(args))
210+
for _, a := range args {
211+
id, err := parseDocID(a)
212+
if err != nil {
213+
return fmt.Errorf("invalid docid %q: %w", a, err)
214+
}
215+
docIDs = append(docIDs, id)
216+
}
217+
218+
req := xpoint.SetApprovalsHiddenRequest{
219+
Hidden: !approvalHiddenShow,
220+
DocID: docIDs,
221+
ProxyUser: approvalHiddenProxyUser,
222+
}
223+
224+
res, err := client.SetApprovalsHidden(cmd.Context(), req)
225+
if err != nil {
226+
return err
227+
}
228+
229+
return render(res, resolveOutputFormat(approvalHiddenOutput), approvalHiddenJQ, func() error {
230+
fmt.Fprintf(os.Stdout, "message: %s\n", res.Message)
231+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
232+
defer w.Flush()
233+
fmt.Fprintln(w, "DOCID")
234+
for _, id := range res.DocID {
235+
fmt.Fprintf(w, "%d\n", id)
236+
}
237+
return nil
238+
})
239+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"method": "PUT",
3+
"path": "/api/v1/approvals/hidden",
4+
"summary": "承認完了書類非表示設定",
5+
"description": "承認完了書類の表示/非表示フラグをまとめて切り替える。承認待ち一覧取得APIで取得した docid を指定すること。承認完了以外の書類を指定するとエラー応答となる。",
6+
"requestBody": {
7+
"type": "object",
8+
"required": ["hidden", "docid"],
9+
"properties": {
10+
"hidden": {
11+
"type": "boolean",
12+
"description": "true: 指定書類を非表示 / false: 指定書類を表示"
13+
},
14+
"docid": {
15+
"type": "array",
16+
"description": "対象の書類ID配列 (承認完了書類のみ有効)",
17+
"items": {
18+
"type": "integer"
19+
}
20+
},
21+
"proxy_user": {
22+
"type": "string",
23+
"description": "代理承認者ユーザCD (代理承認した承認完了書類を対象にするとき指定)"
24+
}
25+
}
26+
},
27+
"response": {
28+
"type": "object",
29+
"properties": {
30+
"docid": {
31+
"type": "array",
32+
"description": "非表示/表示設定した書類ID配列",
33+
"items": {
34+
"type": "integer"
35+
}
36+
},
37+
"message_type": {
38+
"type": "integer",
39+
"description": "メッセージタイプ (3:INFO 固定)"
40+
},
41+
"message": {
42+
"type": "string",
43+
"description": "メッセージ内容"
44+
}
45+
}
46+
}
47+
}

internal/schema/approval.wait.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"method": "GET",
3+
"path": "/api/v1/approvals/wait",
4+
"summary": "承認待ち件数取得",
5+
"description": "PCブラウザのユーザートップ「承認」ガジェット相当の、11種類の承認状況別件数、および最新の承認待ち書類5件の情報を取得する。",
6+
"parameters": [
7+
{
8+
"name": "fgid",
9+
"in": "query",
10+
"type": "integer",
11+
"description": "フォームグループID で絞り込み"
12+
},
13+
{
14+
"name": "fid",
15+
"in": "query",
16+
"type": "integer",
17+
"description": "フォームID で絞り込み (fgid が指定されているときのみ有効)"
18+
},
19+
{
20+
"name": "step",
21+
"in": "query",
22+
"type": "integer",
23+
"description": "ステップ番号で絞り込み (fgid と fid が指定されているときのみ有効)"
24+
}
25+
],
26+
"response": {
27+
"type": "object",
28+
"properties": {
29+
"status_list": {
30+
"type": "array",
31+
"description": "承認状況別の件数リスト",
32+
"items": {
33+
"type": "object",
34+
"properties": {
35+
"type": {
36+
"type": "integer",
37+
"description": "承認状況種別 (0:承認待ち / 1:差し戻され / 2:下書き / 3:保留 / 4:承認中(申請) / 5:差し戻し / 6:承認完了(申請) / 7:却下 / 8:回覧 / 9:承認中(承認) / 10:承認完了(承認))"
38+
},
39+
"name": {
40+
"type": "string",
41+
"description": "承認状況名称"
42+
},
43+
"count": {
44+
"type": "integer",
45+
"description": "件数"
46+
}
47+
}
48+
}
49+
},
50+
"wait_list": {
51+
"type": "array",
52+
"description": "最新の承認待ち書類5件",
53+
"items": {
54+
"type": "object",
55+
"properties": {
56+
"docid": {
57+
"type": "integer",
58+
"description": "書類ID"
59+
},
60+
"name": {
61+
"type": "string",
62+
"description": "フォーム名称"
63+
},
64+
"title": {
65+
"type": "string",
66+
"description": "件名"
67+
},
68+
"writername": {
69+
"type": "string",
70+
"description": "申請者名"
71+
},
72+
"writedate": {
73+
"type": "string",
74+
"description": "申請日時 (YYYY/MM/DD hh:mm:ss)"
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}

internal/schema/schema_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
func TestAliases_Sorted(t *testing.T) {
99
got := Aliases()
1010
want := []string{
11+
"approval.hidden",
1112
"approval.list",
13+
"approval.wait",
1214
"document.attachment.add",
1315
"document.attachment.delete",
1416
"document.attachment.get",

internal/xpoint/client.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,81 @@ func (c *Client) ListApprovals(ctx context.Context, p ApprovalsListParams) (*App
221221
return &out, nil
222222
}
223223

224+
type ApprovalsWaitStatus struct {
225+
Type int `json:"type"`
226+
Name string `json:"name"`
227+
Count int `json:"count"`
228+
}
229+
230+
type ApprovalsWaitItem struct {
231+
DocID int `json:"docid"`
232+
Name string `json:"name"`
233+
Title string `json:"title"`
234+
WriterName string `json:"writername"`
235+
WriteDate string `json:"writedate"`
236+
}
237+
238+
type ApprovalsWaitResponse struct {
239+
StatusList []ApprovalsWaitStatus `json:"status_list"`
240+
WaitList []ApprovalsWaitItem `json:"wait_list"`
241+
}
242+
243+
// ApprovalsWaitParams holds query parameters for GET /api/v1/approvals/wait.
244+
type ApprovalsWaitParams struct {
245+
FormGroupID *int
246+
FormID *int
247+
Step *int
248+
}
249+
250+
func (p ApprovalsWaitParams) query() url.Values {
251+
v := url.Values{}
252+
if p.FormGroupID != nil {
253+
v.Set("fgid", strconv.Itoa(*p.FormGroupID))
254+
}
255+
if p.FormID != nil {
256+
v.Set("fid", strconv.Itoa(*p.FormID))
257+
}
258+
if p.Step != nil {
259+
v.Set("step", strconv.Itoa(*p.Step))
260+
}
261+
return v
262+
}
263+
264+
// GetApprovalsWait calls GET /api/v1/approvals/wait.
265+
func (c *Client) GetApprovalsWait(ctx context.Context, p ApprovalsWaitParams) (*ApprovalsWaitResponse, error) {
266+
var out ApprovalsWaitResponse
267+
if err := c.do(ctx, http.MethodGet, "/api/v1/approvals/wait", p.query(), nil, &out); err != nil {
268+
return nil, err
269+
}
270+
return &out, nil
271+
}
272+
273+
// SetApprovalsHiddenRequest is the PUT /api/v1/approvals/hidden body.
274+
type SetApprovalsHiddenRequest struct {
275+
Hidden bool `json:"hidden"`
276+
DocID []int `json:"docid"`
277+
ProxyUser string `json:"proxy_user,omitempty"`
278+
}
279+
280+
type SetApprovalsHiddenResponse struct {
281+
DocID []int `json:"docid"`
282+
MessageType int `json:"message_type"`
283+
Message string `json:"message"`
284+
}
285+
286+
// SetApprovalsHidden calls PUT /api/v1/approvals/hidden.
287+
func (c *Client) SetApprovalsHidden(ctx context.Context, req SetApprovalsHiddenRequest) (*SetApprovalsHiddenResponse, error) {
288+
body, err := json.Marshal(req)
289+
if err != nil {
290+
return nil, fmt.Errorf("marshal request: %w", err)
291+
}
292+
var out SetApprovalsHiddenResponse
293+
if err := c.do(ctx, http.MethodPut, "/api/v1/approvals/hidden", nil, body, &out); err != nil {
294+
return nil, err
295+
}
296+
return &out, nil
297+
}
298+
224299
type SearchForm struct {
225300
ID int `json:"id"`
226301
Code string `json:"code"`

0 commit comments

Comments
 (0)