Skip to content

Commit

Permalink
Merge pull request #2058 from getAlby/ensure-account-id
Browse files Browse the repository at this point in the history
Ensure account id
  • Loading branch information
bumi authored Feb 4, 2023
2 parents 67e7e2e + ab3e4db commit 81b0260
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/app/screens/Accounts/Show/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function AccountScreen() {
const response = await msg.request<GetAccountRes>("getAccount", {
id,
});
// for backwards compatibility
// TODO: remove. if you ask when, then it's probably now.
if (!response.id) {
response.id = id;
}
setAccount(response);
setAccountName(response.name);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import state from "~/extension/background-script/state";

import migrate from "../index";

afterEach(() => {
jest.clearAllMocks();
});

jest.mock("~/extension/background-script/state");
const mockState = {
saveToStorage: jest.fn,
accounts: {
"8b7f1dc6-ab87-4c6c-bca5-19fa8632731e": {
config: "config-123-456",
connector: "lndhub",
name: "Alby",
nostrPrivateKey: "nostr-123-456",
},
"1e1e8ea6-493e-480b-9855-303d37506e97": {
config: "config-123-456",
connector: "lndhub",
id: "1e1e8ea6-493e-480b-9855-303d37506e97",
name: "Alby",
},
},
};
state.getState = jest.fn().mockReturnValue(mockState);

describe("Ensure account ID", () => {
test("add account ID where missing", async () => {
let accounts = state.getState().accounts;
expect(accounts["8b7f1dc6-ab87-4c6c-bca5-19fa8632731e"].id).toEqual(
undefined
);
await migrate();

accounts = state.getState().accounts;
Object.keys(accounts).forEach((accountId) => {
expect(accounts[accountId].id).toEqual(accountId);
});
});
});
20 changes: 20 additions & 0 deletions src/extension/background-script/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,23 @@ const migrations = {
state.setState({
accounts,
});
// will be persisted by setMigrated
}
},

ensureAccountId: async () => {
const { accounts } = state.getState();
Object.keys(accounts).forEach((accountId) => {
if (!accounts[accountId].id) {
console.info(`updating ${accountId}`);
accounts[accountId].id = accountId;
}
});
state.setState({
accounts,
});
// will be persisted by setMigrated
},
};

const migrate = async () => {
Expand All @@ -48,6 +63,11 @@ const migrate = async () => {
await migrations["migrateisUsingGlobalNostrKey"]();
await setMigrated("migrateisUsingGlobalNostrKey");
}
if (shouldMigrate("ensureAccountId")) {
console.info("Running migration for: ensureAccountId");
await migrations["ensureAccountId"]();
await setMigrated("ensureAccountId");
}
};

export default migrate;

0 comments on commit 81b0260

Please sign in to comment.