2020-07-27 04:51:51 +03:00
|
|
|
import { runQuery } from "~/node_common/data/utilities";
|
|
|
|
|
2020-08-03 02:29:11 +03:00
|
|
|
export default async ({ userId, publicOnly = false }) => {
|
2020-07-27 04:51:51 +03:00
|
|
|
return await runQuery({
|
|
|
|
label: "GET_SLATES_BY_USER_ID",
|
|
|
|
queryFn: async (DB) => {
|
2020-08-25 09:46:02 +03:00
|
|
|
const hasUser = (id) =>
|
|
|
|
DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ ownerId: id })]);
|
|
|
|
const isPublic = () =>
|
|
|
|
DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ public: true })]);
|
2020-07-27 04:51:51 +03:00
|
|
|
|
2020-08-03 02:29:11 +03:00
|
|
|
let query;
|
|
|
|
if (publicOnly) {
|
2020-08-25 09:46:02 +03:00
|
|
|
query = await DB.select("*")
|
|
|
|
.from("slates")
|
|
|
|
.where(hasUser(userId))
|
2020-09-20 21:31:08 +03:00
|
|
|
.where(isPublic())
|
|
|
|
.orderBy("updated_at", "desc");
|
2020-08-03 02:29:11 +03:00
|
|
|
} else {
|
2020-09-20 21:31:08 +03:00
|
|
|
query = await DB.select("*")
|
|
|
|
.from("slates")
|
|
|
|
.where(hasUser(userId))
|
|
|
|
.orderBy("updated_at", "desc");
|
2020-08-03 02:29:11 +03:00
|
|
|
}
|
2020-07-27 04:51:51 +03:00
|
|
|
|
|
|
|
if (!query || query.error) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-08-25 09:46:02 +03:00
|
|
|
return JSON.parse(JSON.stringify(query));
|
2020-07-27 04:51:51 +03:00
|
|
|
},
|
|
|
|
errorFn: async (e) => {
|
2020-09-01 02:47:53 +03:00
|
|
|
console.log({
|
2020-09-13 01:08:36 +03:00
|
|
|
error: true,
|
|
|
|
decorator: "GET_SLATES_BY_USER_ID",
|
2020-09-01 02:47:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return [];
|
2020-07-27 04:51:51 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|