-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
front/pages/api/v1/w/[wId]/data_sources/[name]/documents/[documentId]/parents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getDataSource } from "@app/lib/api/data_sources"; | ||
import { Authenticator, getAPIKey } from "@app/lib/auth"; | ||
import { CoreAPI } from "@app/lib/core_api"; | ||
import { ReturnedAPIErrorType } from "@app/lib/error"; | ||
import { apiError, withLogging } from "@app/logger/withlogging"; | ||
|
||
export type PostParentsResponseBody = { | ||
updated: true; | ||
}; | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<PostParentsResponseBody | ReturnedAPIErrorType> | ||
): Promise<void> { | ||
const keyRes = await getAPIKey(req); | ||
if (keyRes.isErr()) { | ||
return apiError(req, res, keyRes.error); | ||
} | ||
const { auth } = await Authenticator.fromKey( | ||
keyRes.value, | ||
req.query.wId as string | ||
); | ||
|
||
const owner = auth.workspace(); | ||
if (!owner) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_not_found", | ||
message: "The data source you requested was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
const dataSource = await getDataSource(auth, req.query.name as string); | ||
|
||
if (!dataSource) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_not_found", | ||
message: "The data source you requested was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
switch (req.method) { | ||
case "POST": | ||
if (!auth.isBuilder()) { | ||
return apiError(req, res, { | ||
status_code: 403, | ||
api_error: { | ||
type: "data_source_auth_error", | ||
message: | ||
"You can only alter the data souces of the workspaces for which you are a builder.", | ||
}, | ||
}); | ||
} | ||
|
||
if ( | ||
!req.body || | ||
!Array.isArray(req.body.parents) || | ||
!req.body.parents.every((p: any) => typeof p === "string") | ||
) { | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: "Invalid request body, `parents` (string[]) is required.", | ||
}, | ||
}); | ||
} | ||
|
||
const updateRes = await CoreAPI.updateDataSourceDocumentParents({ | ||
projectId: dataSource.dustAPIProjectId, | ||
dataSourceName: dataSource.name, | ||
documentId: req.query.documentId as string, | ||
parents: req.body.parents, | ||
}); | ||
|
||
if (updateRes.isErr()) { | ||
return apiError(req, res, { | ||
status_code: 500, | ||
api_error: { | ||
type: "internal_server_error", | ||
message: "There was an error updating the `parents` field.", | ||
data_source_error: updateRes.error, | ||
}, | ||
}); | ||
} | ||
|
||
res.status(200).json({ updated: true }); | ||
return; | ||
|
||
default: | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: "The method passed is not supported, POST is expected.", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export default withLogging(handler); |