Skip to content

Commit

Permalink
add clientKey in CreateAccountResultOk
Browse files Browse the repository at this point in the history
  • Loading branch information
narumincho committed Jan 22, 2024
1 parent 995e6f3 commit 4f0b07b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
16 changes: 14 additions & 2 deletions server/kv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AccountCode } from "./type/accountCode.ts";
import { AccountDisplayName } from "./type/accountDisplayName.ts";
import { TotpKeyId } from "./type/id.ts";
import { AccountId, TotpKeyId } from "./type/id.ts";
import { TotpSecret } from "./type/totpSecret.ts";
import { ClientKey } from "./type/clientKey.ts";

export type Idea = {
readonly title: string;
Expand Down Expand Up @@ -29,6 +30,11 @@ export type Account = {
readonly displayName: AccountDisplayName;
readonly code: AccountCode;
readonly createDateTime: Date;
readonly clients: ReadonlyArray<{
readonly key: ClientKey;
readonly name: string;
readonly issueDateTime: Date;
}>;
};

export const temporaryTotpKeyKey = (
Expand All @@ -41,4 +47,10 @@ export const cacheAccountByCodeKey = (
accountCode: AccountCode,
) => ["cache", "accountByCode", accountCode];

export type CacheAccountByCodeValue = string;
export type CacheAccountByCodeValue = AccountId;

export const cacheAccountByClientKeyKey = (
clientKey: ClientKey,
) => ["cache", "accountByClientKey", clientKey];

export type CacheAccountByClientKeyValue = AccountId;
18 changes: 17 additions & 1 deletion server/mutation/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
temporaryTotpKeyKey,
TemporaryTotpKeyValue,
} from "../kv.ts";
import { createClientKey } from "../type/clientKey.ts";
import { cacheAccountByClientKeyKey } from "../kv.ts";
import { CacheAccountByClientKeyValue } from "../kv.ts";

export const createAccount: g.GraphQLFieldConfig<
void,
Expand Down Expand Up @@ -81,6 +84,7 @@ export const createAccount: g.GraphQLFieldConfig<
);
const accountId = accountIdFrom(crypto.randomUUID().replaceAll("-", ""));
const createDateTime = new Date();
const clientKey = createClientKey();
const result = await denoKv.atomic().check({
key: cacheAccountByCodeKey(args.accountCode),
versionstamp: null,
Expand All @@ -92,8 +96,19 @@ export const createAccount: g.GraphQLFieldConfig<
code: args.accountCode,
displayName,
createDateTime,
clients: [{
key: clientKey,
name: "initial client",
issueDateTime: createDateTime,
}],
} satisfies Account,
).set(cacheAccountByCodeKey(args.accountCode), accountId).commit();
).set(
cacheAccountByCodeKey(args.accountCode),
accountId satisfies CacheAccountByClientKeyValue,
).set(
cacheAccountByClientKeyKey(clientKey),
accountId satisfies CacheAccountByClientKeyValue,
).commit();
if (!result.ok) {
return {
__typename: "CreateAccountDuplicateCode",
Expand All @@ -110,6 +125,7 @@ export const createAccount: g.GraphQLFieldConfig<
displayName: displayName,
createDateTime,
},
clientKey,
};
},
description: "アカウントを作成する",
Expand Down
6 changes: 6 additions & 0 deletions server/type/createAccountResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as g from "npm:graphql";
import { TotpKeyId } from "./id.ts";
import { AccountCode } from "./accountCode.ts";
import { Account } from "./account.ts";
import { ClientKey } from "./clientKey.ts";

export type CreateAccountDuplicateCode = {
readonly __typename: "CreateAccountDuplicateCode";
Expand Down Expand Up @@ -54,6 +55,7 @@ export const CreateAccountInvalidCode = new g.GraphQLObjectType({
export type CreateAccountResultOk = {
readonly __typename: "CreateAccountResultOk";
readonly account: Account;
readonly clientKey: ClientKey;
};

export const CreateAccountResultOk = new g.GraphQLObjectType({
Expand All @@ -64,6 +66,10 @@ export const CreateAccountResultOk = new g.GraphQLObjectType({
type: new g.GraphQLNonNull(Account),
description: "",
},
clientKey: {
type: new g.GraphQLNonNull(ClientKey),
description: "",
},
},
});

Expand Down

0 comments on commit 4f0b07b

Please sign in to comment.