Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds delete and get subscription route #425

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/api/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import signDataItemModule from "./modules/sign_data_item";
import signDataItem from "./modules/sign_data_item/sign_data_item.background";
import subscriptionModule from "./modules/subscription";
import subscription from "./modules/subscription/subscription.background";
import getSubscription from "./modules/get_subscription/get_subscription.background";
import getSubscriptionModule from "./modules/get_subscription";
import deleteSubscription from "./modules/delete_subscription/delete_subscription.background";
import deleteSubscriptionModule from "./modules/delete_subscription";

/** Background modules */
const modules: BackgroundModule<any>[] = [
Expand All @@ -63,7 +67,9 @@ const modules: BackgroundModule<any>[] = [
{ ...privateHashModule, function: privateHash },
{ ...verifyMessageModule, function: verifyMessage },
{ ...signDataItemModule, function: signDataItem },
{ ...subscriptionModule, function: subscription }
{ ...subscriptionModule, function: subscription },
{ ...getSubscriptionModule, function: getSubscription },
{ ...deleteSubscriptionModule, function: deleteSubscription }
];

export default modules;
Expand Down
7 changes: 7 additions & 0 deletions src/api/foreground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import signMessage, {
finalizer as signMessageFinalizer
} from "./modules/sign_message/sign_message.foreground";
import subscriptionModule from "./modules/subscription";
import getSubscription from "./modules/get_subscription/get_subscription.foreground";
import subscription from "./modules/subscription/subscription.foreground";
import privateHashModule from "./modules/private_hash";
import privateHash, {
Expand All @@ -59,6 +60,9 @@ import signDataItemModule from "./modules/sign_data_item";
import signDataItem, {
finalizer as signDataItemFinalizer
} from "./modules/sign_data_item/sign_data_item.foreground";
import getSubscriptionModule from "./modules/get_subscription";
import deleteSubcription from "./modules/delete_subscription/delete_subscription.foreground";
import deleteSubscriptionModule from "./modules/delete_subscription";

/** Foreground modules */
const modules: ForegroundModule[] = [
Expand Down Expand Up @@ -93,6 +97,9 @@ const modules: ForegroundModule[] = [
function: signDataItem,
finalizer: signDataItemFinalizer
},

{ ...getSubscriptionModule, function: getSubscription },
{ ...deleteSubscriptionModule, function: deleteSubcription },
{ ...subscriptionModule, function: subscription }
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ModuleFunction } from "~api/background";
import { deleteSubscription } from "~subscriptions";
import { getActiveAddress } from "~wallets";

const background: ModuleFunction<{ deleted: boolean }> = async (
_,
arweaveAddress: string
) => {
try {
const activeAddress = await getActiveAddress();
if (!activeAddress) {
throw new Error("No active address found");
}

const status = await deleteSubscription(activeAddress, arweaveAddress);

return { deleted: status };
} catch (err) {
if (err instanceof Error) {
throw new Error(`Failed to fetch subscription data: ${err.message}`);
} else {
throw new Error(
"An unknown error occurred while fetching subscription data"
);
}
}
};

export default background;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ModuleFunction } from "~api/module";

const foreground: ModuleFunction<void> = () => {};

export default foreground;
11 changes: 11 additions & 0 deletions src/api/modules/delete_subscription/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { PermissionType } from "~applications/permissions";
import type { ModuleProperties } from "~api/module";

const permissions: PermissionType[] = ["ACCESS_ADDRESS"];

const deleteSubscriptionModule: ModuleProperties = {
functionName: "deleteSubscription",
permissions
};

export default deleteSubscriptionModule;
30 changes: 30 additions & 0 deletions src/api/modules/get_subscription/get_subscription.background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ModuleFunction } from "~api/background";
import { getSubscription } from "~subscriptions";
import type { SubscriptionData } from "~subscriptions/subscription";
import { getActiveAddress } from "~wallets";

const background: ModuleFunction<SubscriptionData> = async (
_,
arweaveAddress: string
) => {
try {
const activeAddress = await getActiveAddress();
if (!activeAddress) {
throw new Error("No active address found");
}

const subscription = await getSubscription(activeAddress, arweaveAddress);

return subscription;
} catch (err) {
if (err instanceof Error) {
throw new Error(`Failed to fetch subscription data: ${err.message}`);
} else {
throw new Error(
"An unknown error occurred while fetching subscription data"
);
}
}
};

export default background;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ModuleFunction } from "~api/module";

const foreground: ModuleFunction<void> = () => {};

export default foreground;
11 changes: 11 additions & 0 deletions src/api/modules/get_subscription/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { PermissionType } from "~applications/permissions";
import type { ModuleProperties } from "~api/module";

const permissions: PermissionType[] = ["ACCESS_ADDRESS"];

const getSubscriptionModule: ModuleProperties = {
functionName: "getSubscription",
permissions
};

export default getSubscriptionModule;
27 changes: 26 additions & 1 deletion src/subscriptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,32 @@ export async function getSubscriptionData(
}
}

// get singular subscription data from storage
export async function getSubscription(
address: string,
targetId: string
): Promise<SubscriptionData | null> {
try {
if (!address) {
throw new Error("No address provided");
}
const subscriptions = await getSubscriptionData(address);

const subscription = subscriptions.find(
(subscription) => subscription.arweaveAccountAddress === targetId
);

return subscription || null;
} catch (err) {
console.error("Error getting subscription:", err);
return null;
}
}

export async function deleteSubscription(
activeAddress: string,
deleteId: string
) {
): Promise<boolean> {
try {
const subscriptions = await getSubscriptionData(activeAddress);
const subscriptionIndex = subscriptions.findIndex(
Expand All @@ -38,11 +60,14 @@ export async function deleteSubscription(
`subscriptions_${activeAddress}`,
subscriptions
);
return true;
} else {
console.log("No subscription found with the given ID");
return false;
}
} catch (err) {
console.log("Error deleting subscription:", err);
return false;
}
}

Expand Down