user-deletion: elegantly deletes slates and fixes bucket deletion

This commit is contained in:
jimmylee 2020-08-02 15:06:46 -07:00
parent eb38c129be
commit ab462f39c4
3 changed files with 29 additions and 15 deletions

View File

@ -9,6 +9,7 @@ import getSlateByName from "~/node_common/data/methods/get-slate-by-name";
import getSlateById from "~/node_common/data/methods/get-slate-by-id";
import getSlatesByUserId from "~/node_common/data/methods/get-slates-by-user-id";
import updateSlateById from "~/node_common/data/methods/update-slate-by-id";
import deleteSlatesForUserId from "~/node_common/data/methods/delete-slates-for-user-id";
import createAPIKeyForUserId from "~/node_common/data/methods/create-api-key-for-user-id";
import deleteAPIKeyById from "~/node_common/data/methods/delete-api-key-by-id";
@ -30,6 +31,7 @@ export {
getSlateById,
getSlatesByUserId,
updateSlateById,
deleteSlatesForUserId,
// NOTE(jim): API key operations,
createAPIKeyForUserId,
deleteAPIKeyById,

View File

@ -0,0 +1,19 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ userId }) => {
return await runQuery({
label: "DELETE_SLATES_FOR_USER_ID",
queryFn: async (DB) => {
const hasUser = (id) => DB.raw(`?? @> ?::jsonb`, ["data", JSON.stringify({ ownerId: id })]);
const data = await DB.select("*").from("slates").where(hasUser(userId)).del();
return 1 === data;
},
errorFn: async (e) => {
return {
error: "DELETE_SLATES_FOR_USER_ID",
source: e,
};
},
});
};

View File

@ -22,9 +22,7 @@ export default async (req, res) => {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res
.status(500)
.json({ decorator: "SERVER_USER_DELETE", error: true });
return res.status(500).json({ decorator: "SERVER_USER_DELETE", error: true });
}
const user = await Data.getUserById({
@ -32,26 +30,23 @@ export default async (req, res) => {
});
if (!user) {
return res
.status(404)
.json({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
return res.status(404).json({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
}
if (user.error) {
return res
.status(500)
.json({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
return res.status(500).json({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
}
await Data.deleteAPIKeysForUserId({ userId: user.id });
await Data.deleteSlatesForUserId({ userId: user.id });
const i = await Libp2pCryptoIdentity.fromString(user.data.tokens.api);
const b = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
await b.getToken(i);
await b.open("data");
const tokenResponse = await b.getToken(i);
const openResponse = await b.open("data");
try {
const response = await b.remove("data");
const response = await b.remove(openResponse.key);
console.log({ response });
} catch (e) {
console.log(e);
@ -62,9 +57,7 @@ export default async (req, res) => {
});
if (!deleted) {
return res
.status(200)
.json({ decorator: "SERVER_USER_DELETE", error: true });
return res.status(200).json({ decorator: "SERVER_USER_DELETE", error: true });
}
return res.status(200).json({ decorator: "SERVER_USER_DELETE", deleted });