|
1 | | -/** OPENAPI-ROUTE: get-users-me-bugs */ |
2 | | - |
3 | | -import * as db from "@src/features/db"; |
4 | | -import { Context } from "openapi-backend"; |
5 | | - |
6 | | -export default async ( |
7 | | - c: Context, |
8 | | - req: OpenapiRequest, |
9 | | - res: OpenapiResponse |
10 | | -) => { |
11 | | - try { |
12 | | - const sqlCountFields = `SELECT count(b.id) as total`; |
13 | | - const sqlSelectFields = `SELECT |
14 | | - b.id as id, s.id as severityID, s.name as severity, |
15 | | - st.id as statusID, st.name as status, |
16 | | - c.title as campaignTITLE, c.id as campaign, b.message as title`; |
17 | | - |
18 | | - let fromSql = ` |
19 | | - FROM wp_appq_evd_bug b |
20 | | - JOIN wp_appq_evd_campaign c ON (c.id = b.campaign_id) |
21 | | - JOIN wp_appq_evd_severity s ON (s.id = b.severity_id) |
22 | | - JOIN wp_appq_evd_bug_status st ON (st.id = b.status_id) |
23 | | - WHERE b.profile_id = ?`; |
24 | | - |
25 | | - let limits = ""; |
26 | | - const data = [req.user.testerId.toString()]; |
27 | | - const accepted = { |
28 | | - title: ["b.message"], |
29 | | - campaign: ["c.id", "c.title"], |
30 | | - status: ["st.name", "st.id"], |
31 | | - severity: ["s.id", "s.name"], |
32 | | - id: ["b.id"], |
| 1 | +/** OPENAPI-CLASS: get-users-me-bugs */ |
| 2 | + |
| 3 | +import { tryber } from "@src/features/database"; |
| 4 | +import UserRoute from "@src/features/routes/UserRoute"; |
| 5 | + |
| 6 | +export default class Route extends UserRoute<{ |
| 7 | + response: StoplightOperations["get-users-me-bugs"]["responses"]["200"]["content"]["application/json"]; |
| 8 | + query: StoplightOperations["get-users-me-bugs"]["parameters"]["query"]; |
| 9 | +}> { |
| 10 | + private start: number = 0; |
| 11 | + private limit: number | undefined; |
| 12 | + |
| 13 | + private orderBy: NonNullable<Route["query"]>["orderBy"]; |
| 14 | + private order: NonNullable<Route["query"]>["order"] = "DESC"; |
| 15 | + |
| 16 | + private filterBy: |
| 17 | + | { |
| 18 | + campaign?: string; |
| 19 | + title?: string; |
| 20 | + status?: string; |
| 21 | + severity?: string; |
| 22 | + id?: string; |
| 23 | + } |
| 24 | + | false = false; |
| 25 | + |
| 26 | + constructor(configuration: RouteClassConfiguration) { |
| 27 | + super({ ...configuration, element: "bugs" }); |
| 28 | + this.setId(0); |
| 29 | + const query = this.getQuery(); |
| 30 | + if (query.start) this.start = query.start; |
| 31 | + if (query.limit) this.limit = Number(query.limit as unknown as string); |
| 32 | + |
| 33 | + if (query.orderBy) this.orderBy = query.orderBy; |
| 34 | + if (query.order) this.order = query.order; |
| 35 | + |
| 36 | + this.filterBy = this.getFilterBy(); |
| 37 | + } |
| 38 | + private getFilterBy() { |
| 39 | + const query = this.getQuery(); |
| 40 | + if (!query.filterBy) return false; |
| 41 | + |
| 42 | + return { |
| 43 | + ...(query.filterBy?.campaign |
| 44 | + ? { campaign: query.filterBy.campaign as string } |
| 45 | + : {}), |
| 46 | + ...(query.filterBy?.title |
| 47 | + ? { title: query.filterBy.title as string } |
| 48 | + : {}), |
| 49 | + ...(query.filterBy?.status |
| 50 | + ? { status: query.filterBy.status as string } |
| 51 | + : {}), |
| 52 | + ...(query.filterBy?.severity |
| 53 | + ? { status: query.filterBy.severity as string } |
| 54 | + : {}), |
| 55 | + ...(query.filterBy?.id ? { status: query.filterBy.id as string } : {}), |
33 | 56 | }; |
| 57 | + } |
| 58 | + |
| 59 | + protected async prepare() { |
| 60 | + const query = this.getBugsQuery(); |
34 | 61 |
|
35 | | - if (req.query.filterBy) { |
36 | | - for (const [key, value] of Object.entries(req.query.filterBy)) { |
37 | | - if (Object.keys(accepted).includes(key)) { |
38 | | - fromSql += " AND ("; |
39 | | - const orQuery = accepted[key as keyof typeof accepted].map((el) => { |
40 | | - data.push(el, value); |
41 | | - return "?? = ? "; |
42 | | - }); |
43 | | - fromSql += orQuery.join(" OR "); |
44 | | - fromSql += ") "; |
45 | | - } |
| 62 | + this.applyFilterBy(query); |
| 63 | + this.applyOrderBy(query); |
| 64 | + this.applyLimit(query); |
| 65 | + |
| 66 | + const rows = await query; |
| 67 | + if (!rows.length) { |
| 68 | + this.setError(404, Error("Error on finding bugs") as OpenapiError); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + this.setSuccess(200, { |
| 73 | + results: rows.map((bug) => { |
| 74 | + return { |
| 75 | + id: bug.id, |
| 76 | + severity: { |
| 77 | + id: bug.severityID, |
| 78 | + name: bug.severity, |
| 79 | + }, |
| 80 | + status: { |
| 81 | + id: bug.statusID, |
| 82 | + name: bug.status, |
| 83 | + }, |
| 84 | + campaign: { |
| 85 | + id: bug.campaign, |
| 86 | + name: bug.campaignTITLE, |
| 87 | + }, |
| 88 | + title: bug.title, |
| 89 | + }; |
| 90 | + }), |
| 91 | + size: rows.length, |
| 92 | + start: this.start, |
| 93 | + limit: this.limit, |
| 94 | + total: this.limit ? await this.getCount() : undefined, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private applyFilterBy(query: any) { |
| 99 | + if (this.filterBy) { |
| 100 | + if (this.filterBy?.campaign) { |
| 101 | + query |
| 102 | + .where("wp_appq_evd_campaign.id", this.filterBy.campaign) |
| 103 | + .orWhere("wp_appq_evd_campaign.title", this.filterBy.campaign); |
| 104 | + } |
| 105 | + if (this.filterBy?.title) { |
| 106 | + query.where("wp_appq_evd_bug.title", this.filterBy.title); |
| 107 | + } |
| 108 | + if (this.filterBy?.status) { |
| 109 | + query |
| 110 | + .where("wp_appq_evd_bug_status.name", this.filterBy.status) |
| 111 | + .orWhere("wp_appq_evd_bug_status.id", this.filterBy.status); |
| 112 | + } |
| 113 | + if (this.filterBy?.severity) { |
| 114 | + query |
| 115 | + .where("wp_appq_evd_severity.name", this.filterBy.severity) |
| 116 | + .orWhere("wp_appq_evd_severity.id", this.filterBy.severity); |
46 | 117 | } |
47 | 118 | } |
| 119 | + } |
48 | 120 |
|
49 | | - if (req.query.orderBy) { |
50 | | - let orderBy = "b.id"; |
51 | | - if (req.query.orderBy == "id") orderBy = "b.id"; |
52 | | - if (req.query.orderBy == "campaign") orderBy = "c.id"; |
53 | | - if (req.query.orderBy == "status") orderBy = "st.id"; |
54 | | - if (req.query.orderBy == "title") orderBy = "b.message"; |
55 | | - fromSql += ` ORDER BY ?? ${req.query.order || "DESC"}`; |
56 | | - data.push(orderBy); |
| 121 | + private applyOrderBy(query: any) { |
| 122 | + if (this.orderBy) { |
| 123 | + switch (this.orderBy) { |
| 124 | + case "campaign": |
| 125 | + query.orderBy("wp_appq_evd_campaign.id", this.order); |
| 126 | + break; |
| 127 | + case "status": |
| 128 | + query.orderBy("wp_appq_evd_bug_status.id", this.order); |
| 129 | + break; |
| 130 | + case "title": |
| 131 | + query.orderBy("wp_appq_evd_bug.message", this.order); |
| 132 | + break; |
| 133 | + default: |
| 134 | + query.orderBy("wp_appq_evd_bug.id", this.order); |
| 135 | + } |
57 | 136 | } |
| 137 | + } |
58 | 138 |
|
59 | | - const limitData: number[] = []; |
60 | | - if (req.query.limit && typeof req.query.limit === "string") { |
61 | | - limits += " LIMIT ? "; |
62 | | - limitData.push(parseInt(req.query.limit)); |
63 | | - if (req.query.start && typeof req.query.start === "string") { |
64 | | - limits += " OFFSET ? "; |
65 | | - limitData.push(parseInt(req.query.start)); |
| 139 | + private applyLimit(query: any) { |
| 140 | + if (this.limit) { |
| 141 | + query.limit(this.limit); |
| 142 | + if (this.start) { |
| 143 | + query.offset(this.start); |
66 | 144 | } |
67 | 145 | } |
| 146 | + } |
68 | 147 |
|
69 | | - const getBugsSql = sqlSelectFields + fromSql + limits; |
70 | | - let getBugsQuery = db.format(getBugsSql, data); |
71 | | - getBugsQuery = db.format(getBugsQuery, limitData); |
| 148 | + private getBugsQuery() { |
| 149 | + const query = tryber.tables.WpAppqEvdBug.do() |
| 150 | + .select( |
| 151 | + tryber.ref("id").withSchema("wp_appq_evd_bug"), |
| 152 | + tryber |
| 153 | + .ref("severity_id") |
| 154 | + .withSchema("wp_appq_evd_bug") |
| 155 | + .as("severityID"), |
| 156 | + tryber.ref("status_id").withSchema("wp_appq_evd_bug").as("statusID"), |
| 157 | + tryber.ref("message").withSchema("wp_appq_evd_bug").as("title"), |
| 158 | + tryber.ref("campaign_id").withSchema("wp_appq_evd_bug").as("campaign") |
| 159 | + ) |
| 160 | + .join( |
| 161 | + "wp_appq_evd_bug_status", |
| 162 | + "wp_appq_evd_bug.status_id", |
| 163 | + "wp_appq_evd_bug_status.id" |
| 164 | + ) |
| 165 | + .select( |
| 166 | + tryber.ref("name").withSchema("wp_appq_evd_bug_status").as("status") |
| 167 | + ) |
| 168 | + .join( |
| 169 | + "wp_appq_evd_severity", |
| 170 | + "wp_appq_evd_bug.severity_id", |
| 171 | + "wp_appq_evd_severity.id" |
| 172 | + ) |
| 173 | + .select( |
| 174 | + tryber.ref("name").withSchema("wp_appq_evd_severity").as("severity") |
| 175 | + ) |
| 176 | + .join( |
| 177 | + "wp_appq_evd_campaign", |
| 178 | + "wp_appq_evd_bug.campaign_id", |
| 179 | + "wp_appq_evd_campaign.id" |
| 180 | + ) |
| 181 | + .select( |
| 182 | + tryber |
| 183 | + .ref("title") |
| 184 | + .withSchema("wp_appq_evd_campaign") |
| 185 | + .as("campaignTITLE") |
| 186 | + ) |
| 187 | + .where("wp_appq_evd_bug.profile_id", this.getTesterId()); |
72 | 188 |
|
73 | | - const rows = await db.query(getBugsQuery); |
74 | | - if (!rows.length) { |
75 | | - throw Error("Error on finding bugs"); |
76 | | - } |
| 189 | + return query; |
| 190 | + } |
77 | 191 |
|
78 | | - let bugList = rows.map(mapQueryToObject); |
79 | | - let start = 0; |
80 | | - if (req.query.start && typeof req.query.start === "string") { |
81 | | - start = parseInt(req.query.start); |
82 | | - } |
| 192 | + private async getCount() { |
| 193 | + const query = tryber.tables.WpAppqEvdBug.do() |
| 194 | + .count({ |
| 195 | + total: tryber.ref("id").withSchema("wp_appq_evd_bug"), |
| 196 | + }) |
| 197 | + .join( |
| 198 | + "wp_appq_evd_bug_status", |
| 199 | + "wp_appq_evd_bug.status_id", |
| 200 | + "wp_appq_evd_bug_status.id" |
| 201 | + ) |
| 202 | + .join( |
| 203 | + "wp_appq_evd_severity", |
| 204 | + "wp_appq_evd_bug.severity_id", |
| 205 | + "wp_appq_evd_severity.id" |
| 206 | + ) |
| 207 | + .join( |
| 208 | + "wp_appq_evd_campaign", |
| 209 | + "wp_appq_evd_bug.campaign_id", |
| 210 | + "wp_appq_evd_campaign.id" |
| 211 | + ) |
| 212 | + .where("wp_appq_evd_bug.profile_id", this.getTesterId()); |
83 | 213 |
|
84 | | - const results: { |
85 | | - results: typeof bugList; |
86 | | - size: number; |
87 | | - total?: number; |
88 | | - start: number; |
89 | | - limit?: number; |
90 | | - } = { |
91 | | - results: bugList, |
92 | | - size: rows.length, |
93 | | - start: start, |
94 | | - }; |
95 | | - if (req.query.limit && typeof req.query.limit === "string") { |
96 | | - results.limit = parseInt(req.query.limit); |
97 | | - const getBugsCountSql = sqlCountFields + fromSql; |
98 | | - const getBugsCountQuery = db.format(getBugsCountSql, data); |
99 | | - |
100 | | - const countRows = await db.query(getBugsCountQuery); |
101 | | - if (!countRows.length) { |
102 | | - throw Error("Error on finding bugs total"); |
103 | | - } |
104 | | - results.total = countRows[0].total; |
| 214 | + this.applyFilterBy(query); |
| 215 | + |
| 216 | + const countRows = await query; |
| 217 | + if (!countRows.length) { |
| 218 | + throw Error("Error on finding bugs total"); |
105 | 219 | } |
106 | | - res.status_code = 200; |
107 | | - return results; |
108 | | - } catch (error) { |
109 | | - if (process.env && process.env.DEBUG) console.log(error); |
110 | | - res.status_code = 404; |
111 | | - return { |
112 | | - element: "bugs", |
113 | | - id: 0, |
114 | | - message: (error as OpenapiError).message, |
115 | | - }; |
| 220 | + return Number(countRows[0].total); |
116 | 221 | } |
117 | | -}; |
118 | | - |
119 | | -const mapQueryToObject = (bug: { |
120 | | - id: string; |
121 | | - severityID: string; |
122 | | - severity: string; |
123 | | - statusID: string; |
124 | | - status: string; |
125 | | - campaignTITLE: string; |
126 | | - campaign: string; |
127 | | - title: string; |
128 | | -}) => { |
129 | | - return { |
130 | | - id: bug.id, |
131 | | - severity: { |
132 | | - id: bug.severityID, |
133 | | - name: bug.severity, |
134 | | - }, |
135 | | - status: { |
136 | | - id: bug.statusID, |
137 | | - name: bug.status, |
138 | | - }, |
139 | | - campaign: { |
140 | | - id: bug.campaign, |
141 | | - name: bug.campaignTITLE, |
142 | | - }, |
143 | | - title: bug.title, |
144 | | - }; |
145 | | -}; |
| 222 | +} |
0 commit comments