71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
import { Drash, ogs, z } from "./deps.ts";
|
|
import PgConn from "./database.ts";
|
|
import { validateBody } from "./util.ts";
|
|
|
|
const PostNews = z.object({
|
|
news_id: z.string({ required_error: 'news_id is required'}),
|
|
url: z.string({ required_error: 'url is required'}).url("Url is not valid"),
|
|
user_id: z.string({ required_error: 'user_id is required'})
|
|
})
|
|
|
|
interface PostNewsRequest {
|
|
news_id: string,
|
|
url: string,
|
|
user_id: string
|
|
}
|
|
|
|
export default class HomeResource extends Drash.Resource {
|
|
public paths = ["/api/news-event/approve"];
|
|
|
|
public async POST(request: Drash.Request, response: Drash.Response): Promise<void> {
|
|
const body = request.bodyAll() as PostNewsRequest;
|
|
|
|
try {
|
|
validateBody(PostNews, body, response)
|
|
} catch (error) {
|
|
return response.json(error, 400)
|
|
}
|
|
|
|
const options = { url: body.url }
|
|
|
|
try {
|
|
|
|
const isAdmin = await PgConn`
|
|
SELECT is_admin
|
|
FROM USERS
|
|
WHERE id = ${body.user_id}
|
|
`
|
|
|
|
if(!isAdmin[0].is_admin) {
|
|
return response.json({
|
|
message: 'Forbidden'
|
|
}, 403)
|
|
}
|
|
|
|
|
|
const { result } = await ogs.default(options)
|
|
let thumbnail = ''
|
|
|
|
if(result.ogImage) {
|
|
thumbnail = result.ogImage[0].url
|
|
}
|
|
|
|
await PgConn`
|
|
UPDATE news_events
|
|
SET approved_by = ${body.user_id}, thumbnail = ${thumbnail}
|
|
WHERE id = ${body.news_id}
|
|
RETURNING *
|
|
`
|
|
|
|
return response.json({} , 201)
|
|
|
|
|
|
} catch (error) {
|
|
console.log(error)
|
|
return response.json({
|
|
message: "Something went wrong while try to save news / event",
|
|
error: error
|
|
});
|
|
}
|
|
}
|
|
} |