@terreno/ai 57.4.0 → 57.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.GptHistory = void 0;
7
- var api_1 = require("@terreno/api");
8
- var mongoose_1 = __importDefault(require("mongoose"));
9
- var contentPartSchema = new mongoose_1.default.Schema({
7
+ const api_1 = require("@terreno/api");
8
+ const mongoose_1 = __importDefault(require("mongoose"));
9
+ const contentPartSchema = new mongoose_1.default.Schema({
10
10
  filename: { description: "Original filename of the attached file", type: String },
11
11
  mimeType: { description: "MIME type of the content part", type: String },
12
12
  text: { description: "Text content of this part", type: String },
@@ -18,7 +18,7 @@ var contentPartSchema = new mongoose_1.default.Schema({
18
18
  },
19
19
  url: { description: "URL pointing to the content resource", type: String },
20
20
  }, { _id: false, strict: "throw" });
21
- var gptHistoryPromptSchema = new mongoose_1.default.Schema({
21
+ const gptHistoryPromptSchema = new mongoose_1.default.Schema({
22
22
  args: { description: "Arguments passed to a tool call", type: mongoose_1.default.Schema.Types.Mixed },
23
23
  content: { description: "Multipart content attached to this prompt", type: [contentPartSchema] },
24
24
  model: { description: "AI model identifier used for this prompt", type: String },
@@ -41,7 +41,7 @@ var gptHistoryPromptSchema = new mongoose_1.default.Schema({
41
41
  type: String,
42
42
  },
43
43
  }, { _id: false, strict: "throw" });
44
- var gptHistorySchema = new mongoose_1.default.Schema({
44
+ const gptHistorySchema = new mongoose_1.default.Schema({
45
45
  projectId: {
46
46
  description: "Project this conversation belongs to",
47
47
  index: true,
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Project = void 0;
7
- var api_1 = require("@terreno/api");
8
- var mongoose_1 = __importDefault(require("mongoose"));
9
- var memorySchema = new mongoose_1.default.Schema({
7
+ const api_1 = require("@terreno/api");
8
+ const mongoose_1 = __importDefault(require("mongoose"));
9
+ const memorySchema = new mongoose_1.default.Schema({
10
10
  category: { description: "Optional grouping category for this memory", type: String },
11
11
  source: {
12
12
  default: "user",
@@ -16,7 +16,7 @@ var memorySchema = new mongoose_1.default.Schema({
16
16
  },
17
17
  text: { description: "The memory content", required: true, type: String },
18
18
  }, { strict: "throw", timestamps: { createdAt: "created", updatedAt: false } });
19
- var projectSchema = new mongoose_1.default.Schema({
19
+ const projectSchema = new mongoose_1.default.Schema({
20
20
  memories: {
21
21
  default: [],
22
22
  description: "Persistent memories for this project",
@@ -1,78 +1,25 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __read = (this && this.__read) || function (o, n) {
39
- var m = typeof Symbol === "function" && o[Symbol.iterator];
40
- if (!m) return o;
41
- var i = m.call(o), r, ar = [], e;
42
- try {
43
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
- }
45
- catch (error) { e = { error: error }; }
46
- finally {
47
- try {
48
- if (r && !r.done && (m = i["return"])) m.call(i);
49
- }
50
- finally { if (e) throw e.error; }
51
- }
52
- return ar;
53
- };
54
2
  Object.defineProperty(exports, "__esModule", { value: true });
55
3
  exports.addAiRequestsExplorerRoutes = void 0;
56
- var api_1 = require("@terreno/api");
57
- var luxon_1 = require("luxon");
58
- var aiRequest_1 = require("../models/aiRequest");
59
- var parseRequestTypes = function (raw) {
4
+ const api_1 = require("@terreno/api");
5
+ const luxon_1 = require("luxon");
6
+ const aiRequest_1 = require("../models/aiRequest");
7
+ const parseRequestTypes = (raw) => {
60
8
  if (Array.isArray(raw)) {
61
- return raw.flatMap(function (item) { return parseRequestTypes(item); });
9
+ return raw.flatMap((item) => parseRequestTypes(item));
62
10
  }
63
11
  if (typeof raw !== "string") {
64
12
  return [];
65
13
  }
66
14
  return raw
67
15
  .split(",")
68
- .map(function (token) { return token.trim(); })
69
- .filter(function (token) { return token.length > 0; });
16
+ .map((token) => token.trim())
17
+ .filter((token) => token.length > 0);
70
18
  };
71
- var addAiRequestsExplorerRoutes = function (router, options) {
72
- var _a;
19
+ const addAiRequestsExplorerRoutes = (router, options) => {
73
20
  router.get("/aiRequestsExplorer", [
74
21
  (0, api_1.authenticateMiddleware)(),
75
- (0, api_1.createOpenApiBuilder)((_a = options === null || options === void 0 ? void 0 : options.openApiOptions) !== null && _a !== void 0 ? _a : {})
22
+ (0, api_1.createOpenApiBuilder)(options?.openApiOptions ?? {})
76
23
  .withTags(["admin"])
77
24
  .withSummary("List AI requests (admin only)")
78
25
  .withQueryParameter("page", { type: "number" }, { required: false })
@@ -89,95 +36,87 @@ var addAiRequestsExplorerRoutes = function (router, options) {
89
36
  total: { type: "number" },
90
37
  })
91
38
  .build(),
92
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
93
- var user, page, limit, skip, match, requestTypes, startDt, endDt, _a, results, totalCount;
94
- return __generator(this, function (_b) {
95
- switch (_b.label) {
96
- case 0:
97
- user = req.user;
98
- if (!(user === null || user === void 0 ? void 0 : user.admin)) {
99
- return [2 /*return*/, res.status(403).json({ error: "Admin access required" })];
100
- }
101
- page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
102
- limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
103
- skip = (page - 1) * limit;
104
- match = {
105
- deleted: { $ne: true },
106
- };
107
- requestTypes = parseRequestTypes(req.query.requestType);
108
- if (requestTypes.length === 1) {
109
- match.requestType = requestTypes[0];
110
- }
111
- else if (requestTypes.length > 1) {
112
- match.requestType = { $in: requestTypes };
113
- }
114
- if (req.query.model) {
115
- match.aiModel = req.query.model;
116
- }
117
- if (req.query.startDate || req.query.endDate) {
118
- match.created = {};
119
- if (req.query.startDate) {
120
- startDt = luxon_1.DateTime.fromISO(req.query.startDate, { zone: "utc" });
121
- if (!startDt.isValid) {
122
- throw new api_1.APIError({
123
- status: 400,
124
- title: "Invalid startDate format (expected ISO 8601)",
125
- });
126
- }
127
- match.created.$gte = startDt.toJSDate();
128
- }
129
- if (req.query.endDate) {
130
- endDt = luxon_1.DateTime.fromISO(req.query.endDate, { zone: "utc" });
131
- if (!endDt.isValid) {
132
- throw new api_1.APIError({ status: 400, title: "Invalid endDate format (expected ISO 8601)" });
133
- }
134
- match.created.$lte = endDt.toJSDate();
135
- }
136
- }
137
- return [4 /*yield*/, Promise.all([
138
- aiRequest_1.AIRequest.aggregate([
139
- { $match: match },
140
- { $sort: { created: -1 } },
141
- { $skip: skip },
142
- { $limit: limit },
143
- {
144
- $lookup: {
145
- as: "user",
146
- foreignField: "_id",
147
- from: "users",
148
- localField: "userId",
149
- },
150
- },
151
- { $unwind: { path: "$user", preserveNullAndEmptyArrays: true } },
152
- {
153
- $project: {
154
- aiModel: 1,
155
- created: 1,
156
- error: 1,
157
- prompt: { $substrCP: ["$prompt", 0, 200] },
158
- requestType: 1,
159
- response: { $substrCP: [{ $ifNull: ["$response", ""] }, 0, 200] },
160
- responseTime: 1,
161
- tokensUsed: 1,
162
- "user.email": 1,
163
- "user.name": 1,
164
- userId: 1,
165
- },
166
- },
167
- ]),
168
- aiRequest_1.AIRequest.countDocuments(match),
169
- ])];
170
- case 1:
171
- _a = __read.apply(void 0, [_b.sent(), 2]), results = _a[0], totalCount = _a[1];
172
- return [2 /*return*/, res.json({
173
- data: results,
174
- limit: limit,
175
- more: skip + limit < totalCount,
176
- page: page,
177
- total: totalCount,
178
- })];
39
+ ], (0, api_1.asyncHandler)(async (req, res) => {
40
+ const user = req.user;
41
+ if (!user?.admin) {
42
+ return res.status(403).json({ error: "Admin access required" });
43
+ }
44
+ const page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
45
+ const limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
46
+ const skip = (page - 1) * limit;
47
+ const match = {
48
+ deleted: { $ne: true },
49
+ };
50
+ const requestTypes = parseRequestTypes(req.query.requestType);
51
+ if (requestTypes.length === 1) {
52
+ match.requestType = requestTypes[0];
53
+ }
54
+ else if (requestTypes.length > 1) {
55
+ match.requestType = { $in: requestTypes };
56
+ }
57
+ if (req.query.model) {
58
+ match.aiModel = req.query.model;
59
+ }
60
+ if (req.query.startDate || req.query.endDate) {
61
+ match.created = {};
62
+ if (req.query.startDate) {
63
+ const startDt = luxon_1.DateTime.fromISO(req.query.startDate, { zone: "utc" });
64
+ if (!startDt.isValid) {
65
+ throw new api_1.APIError({
66
+ status: 400,
67
+ title: "Invalid startDate format (expected ISO 8601)",
68
+ });
69
+ }
70
+ match.created.$gte = startDt.toJSDate();
71
+ }
72
+ if (req.query.endDate) {
73
+ const endDt = luxon_1.DateTime.fromISO(req.query.endDate, { zone: "utc" });
74
+ if (!endDt.isValid) {
75
+ throw new api_1.APIError({ status: 400, title: "Invalid endDate format (expected ISO 8601)" });
76
+ }
77
+ match.created.$lte = endDt.toJSDate();
179
78
  }
79
+ }
80
+ const [results, totalCount] = await Promise.all([
81
+ aiRequest_1.AIRequest.aggregate([
82
+ { $match: match },
83
+ { $sort: { created: -1 } },
84
+ { $skip: skip },
85
+ { $limit: limit },
86
+ {
87
+ $lookup: {
88
+ as: "user",
89
+ foreignField: "_id",
90
+ from: "users",
91
+ localField: "userId",
92
+ },
93
+ },
94
+ { $unwind: { path: "$user", preserveNullAndEmptyArrays: true } },
95
+ {
96
+ $project: {
97
+ aiModel: 1,
98
+ created: 1,
99
+ error: 1,
100
+ prompt: { $substrCP: ["$prompt", 0, 200] },
101
+ requestType: 1,
102
+ response: { $substrCP: [{ $ifNull: ["$response", ""] }, 0, 200] },
103
+ responseTime: 1,
104
+ tokensUsed: 1,
105
+ "user.email": 1,
106
+ "user.name": 1,
107
+ userId: 1,
108
+ },
109
+ },
110
+ ]),
111
+ aiRequest_1.AIRequest.countDocuments(match),
112
+ ]);
113
+ return res.json({
114
+ data: results,
115
+ limit,
116
+ more: skip + limit < totalCount,
117
+ page,
118
+ total: totalCount,
180
119
  });
181
- }); }));
120
+ }));
182
121
  };
183
122
  exports.addAiRequestsExplorerRoutes = addAiRequestsExplorerRoutes;
@@ -1,49 +1,13 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
6
  exports.addFileRoutes = void 0;
43
- var api_1 = require("@terreno/api");
44
- var multer_1 = __importDefault(require("multer"));
45
- var fileAttachment_1 = require("../models/fileAttachment");
46
- var ALLOWED_MIME_TYPES = new Set([
7
+ const api_1 = require("@terreno/api");
8
+ const multer_1 = __importDefault(require("multer"));
9
+ const fileAttachment_1 = require("../models/fileAttachment");
10
+ const ALLOWED_MIME_TYPES = new Set([
47
11
  "image/jpeg",
48
12
  "image/png",
49
13
  "image/gif",
@@ -53,17 +17,16 @@ var ALLOWED_MIME_TYPES = new Set([
53
17
  "text/csv",
54
18
  "application/json",
55
19
  ]);
56
- var DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
57
- var addFileRoutes = function (router, options) {
58
- var _a, _b, _c;
59
- var fileStorageService = options.fileStorageService, _d = options.maxFileSize, maxFileSize = _d === void 0 ? DEFAULT_MAX_FILE_SIZE : _d;
60
- var upload = (0, multer_1.default)({
61
- fileFilter: function (_req, file, cb) {
20
+ const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
21
+ const addFileRoutes = (router, options) => {
22
+ const { fileStorageService, maxFileSize = DEFAULT_MAX_FILE_SIZE } = options;
23
+ const upload = (0, multer_1.default)({
24
+ fileFilter: (_req, file, cb) => {
62
25
  if (ALLOWED_MIME_TYPES.has(file.mimetype)) {
63
26
  cb(null, true);
64
27
  }
65
28
  else {
66
- cb(new Error("File type ".concat(file.mimetype, " is not allowed")));
29
+ cb(new Error(`File type ${file.mimetype} is not allowed`));
67
30
  }
68
31
  },
69
32
  limits: { fileSize: maxFileSize },
@@ -72,7 +35,7 @@ var addFileRoutes = function (router, options) {
72
35
  router.post("/files/upload", [
73
36
  (0, api_1.authenticateMiddleware)(),
74
37
  upload.single("file"),
75
- (0, api_1.createOpenApiBuilder)((_a = options.openApiOptions) !== null && _a !== void 0 ? _a : {})
38
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
76
39
  .withTags(["files"])
77
40
  .withSummary("Upload a file")
78
41
  .withResponse(200, {
@@ -83,86 +46,58 @@ var addFileRoutes = function (router, options) {
83
46
  url: { type: "string" },
84
47
  })
85
48
  .build(),
86
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
87
- var file, userId, result;
88
- var _a;
89
- return __generator(this, function (_b) {
90
- switch (_b.label) {
91
- case 0:
92
- file = req.file;
93
- userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a._id;
94
- if (!file) {
95
- throw new api_1.APIError({ status: 400, title: "No file provided" });
96
- }
97
- return [4 /*yield*/, fileStorageService.upload({
98
- buffer: file.buffer,
99
- filename: file.originalname,
100
- mimeType: file.mimetype,
101
- userId: userId,
102
- })];
103
- case 1:
104
- result = _b.sent();
105
- return [2 /*return*/, res.json({ data: result })];
106
- }
49
+ ], (0, api_1.asyncHandler)(async (req, res) => {
50
+ const file = req.file;
51
+ const userId = req.user
52
+ ?._id;
53
+ if (!file) {
54
+ throw new api_1.APIError({ status: 400, title: "No file provided" });
55
+ }
56
+ const result = await fileStorageService.upload({
57
+ buffer: file.buffer,
58
+ filename: file.originalname,
59
+ mimeType: file.mimetype,
60
+ userId,
107
61
  });
108
- }); }));
62
+ return res.json({ data: result });
63
+ }));
109
64
  router.get("/files/*gcsKey", [
110
- (0, api_1.createOpenApiBuilder)((_b = options.openApiOptions) !== null && _b !== void 0 ? _b : {})
65
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
111
66
  .withTags(["files"])
112
67
  .withSummary("Get file URL")
113
68
  .withPathParameter("gcsKey", { type: "string" })
114
69
  .withResponse(200, { url: { type: "string" } })
115
70
  .build(),
116
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
117
- var gcsKey, attachment, url;
118
- return __generator(this, function (_a) {
119
- switch (_a.label) {
120
- case 0:
121
- gcsKey = req.params.gcsKey;
122
- return [4 /*yield*/, fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey: gcsKey })];
123
- case 1:
124
- attachment = _a.sent();
125
- if (!attachment) {
126
- throw new api_1.APIError({ status: 404, title: "File not found" });
127
- }
128
- return [4 /*yield*/, fileStorageService.getSignedUrl(gcsKey)];
129
- case 2:
130
- url = _a.sent();
131
- return [2 /*return*/, res.json({ data: { url: url } })];
132
- }
133
- });
134
- }); }));
71
+ ], (0, api_1.asyncHandler)(async (req, res) => {
72
+ const gcsKey = req.params.gcsKey;
73
+ const attachment = await fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey });
74
+ if (!attachment) {
75
+ throw new api_1.APIError({ status: 404, title: "File not found" });
76
+ }
77
+ const url = await fileStorageService.getSignedUrl(gcsKey);
78
+ return res.json({ data: { url } });
79
+ }));
135
80
  router.delete("/files/*gcsKey", [
136
81
  (0, api_1.authenticateMiddleware)(),
137
- (0, api_1.createOpenApiBuilder)((_c = options.openApiOptions) !== null && _c !== void 0 ? _c : {})
82
+ (0, api_1.createOpenApiBuilder)(options.openApiOptions ?? {})
138
83
  .withTags(["files"])
139
84
  .withSummary("Delete a file")
140
85
  .withPathParameter("gcsKey", { type: "string" })
141
86
  .withResponse(200, { success: { type: "boolean" } })
142
87
  .build(),
143
- ], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
144
- var gcsKey, userId, attachment;
145
- var _a;
146
- return __generator(this, function (_b) {
147
- switch (_b.label) {
148
- case 0:
149
- gcsKey = req.params.gcsKey;
150
- userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a._id;
151
- return [4 /*yield*/, fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey: gcsKey })];
152
- case 1:
153
- attachment = _b.sent();
154
- if (!attachment) {
155
- throw new api_1.APIError({ status: 404, title: "File not found" });
156
- }
157
- if (attachment.userId.toString() !== userId.toString()) {
158
- throw new api_1.APIError({ status: 403, title: "Not authorized to delete this file" });
159
- }
160
- return [4 /*yield*/, fileStorageService.delete(gcsKey)];
161
- case 2:
162
- _b.sent();
163
- return [2 /*return*/, res.json({ data: { success: true } })];
164
- }
165
- });
166
- }); }));
88
+ ], (0, api_1.asyncHandler)(async (req, res) => {
89
+ const gcsKey = req.params.gcsKey;
90
+ const userId = req.user
91
+ ?._id;
92
+ const attachment = await fileAttachment_1.FileAttachment.findOneOrNone({ deleted: false, gcsKey });
93
+ if (!attachment) {
94
+ throw new api_1.APIError({ status: 404, title: "File not found" });
95
+ }
96
+ if (attachment.userId.toString() !== userId.toString()) {
97
+ throw new api_1.APIError({ status: 403, title: "Not authorized to delete this file" });
98
+ }
99
+ await fileStorageService.delete(gcsKey);
100
+ return res.json({ data: { success: true } });
101
+ }));
167
102
  };
168
103
  exports.addFileRoutes = addFileRoutes;