-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
972 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
apps/server/prisma/migrations/20230322181621_add_invite/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-- CreateTable | ||
CREATE TABLE "Invite" ( | ||
"inviteId" VARCHAR(100) NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"createdById" INTEGER, | ||
"usedById" INTEGER, | ||
"tenantId" INTEGER, | ||
"usedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "Invite_pkey" PRIMARY KEY ("inviteId") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Invite" ADD CONSTRAINT "Invite_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Invite" ADD CONSTRAINT "Invite_usedById_fkey" FOREIGN KEY ("usedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Invite" ADD CONSTRAINT "Invite_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Methods } from 'api-types/api/v1/invites'; | ||
import { invites } from '../../../models'; | ||
import { | ||
InviteDisabledError, | ||
NoTenantError, | ||
TooManyInvitesError | ||
} from '../../../models/invite'; | ||
import { APIRoute, UserState } from '../../../utils/types'; | ||
|
||
type Response = Methods['post']['resBody']; | ||
|
||
export const postV1Invites: APIRoute< | ||
never, | ||
never, | ||
never, | ||
Response, | ||
UserState | ||
> = async ctx => { | ||
try { | ||
const invite = await invites.createInvite(ctx.state.user); | ||
|
||
ctx.body = { | ||
invite: invites.getPublic(invite) | ||
}; | ||
} catch (e) { | ||
if (e instanceof TooManyInvitesError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '招待コードは5個まで発行できます。' | ||
}; | ||
return; | ||
} | ||
|
||
if (e instanceof NoTenantError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '招待コードを作成するには、既に配信者である必要があります。' | ||
}; | ||
return; | ||
} | ||
|
||
if (e instanceof InviteDisabledError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '招待コードの新規作成は現在管理者が無効にしています。' | ||
}; | ||
return; | ||
} | ||
|
||
throw e; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Methods } from 'api-types/api/v1/invites'; | ||
import { invites } from '../../../models'; | ||
import { APIRoute, UserState } from '../../../utils/types'; | ||
|
||
type Response = Methods['get']['resBody']; | ||
|
||
export const getV1InvitesGetList: APIRoute< | ||
never, | ||
never, | ||
never, | ||
Response, | ||
UserState | ||
> = async ctx => { | ||
const list = await invites.getList(ctx.state.user.id); | ||
const publicList = list.map(invites.getPublic); | ||
|
||
ctx.body = publicList; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { JSONSchemaType } from 'ajv'; | ||
import { Methods } from 'api-types/api/v1/tenants'; | ||
import { tenants } from '../../../models'; | ||
import { | ||
InvalidSlugError, | ||
InviteAlreadyUsedError, | ||
NoInviteError, | ||
SlugAlreadyUsedError | ||
} from '../../../models/tenant'; | ||
import { APIRoute, UserState } from '../../../utils/types'; | ||
import { validateWithType } from '../../../utils/validate'; | ||
|
||
type Request = Methods['post']['reqBody']; | ||
type Response = Methods['post']['resBody']; | ||
|
||
const reqBodySchema: JSONSchemaType<Request> = { | ||
type: 'object', | ||
properties: { | ||
inviteCode: { | ||
type: 'string', | ||
minLength: 1, | ||
maxLength: 100 | ||
}, | ||
slug: { | ||
type: 'string', | ||
minLength: 1, | ||
maxLength: 100 | ||
} | ||
}, | ||
required: ['inviteCode', 'slug'], | ||
additionalProperties: false | ||
}; | ||
|
||
export const postV1Tenants: APIRoute< | ||
never, | ||
never, | ||
Request, | ||
Response, | ||
UserState | ||
> = async ctx => { | ||
if (!validateWithType(reqBodySchema, ctx.request.body)) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request' | ||
}; | ||
return; | ||
} | ||
const body = ctx.request.body; | ||
const newSlug = body.slug?.toLowerCase(); | ||
|
||
try { | ||
const tenant = await tenants.createTenantByInvite( | ||
newSlug, | ||
ctx.state.user, | ||
body.inviteCode | ||
); | ||
|
||
ctx.body = { | ||
tenant: tenants.getPublic(tenant) | ||
}; | ||
} catch (e) { | ||
if (e instanceof InvalidSlugError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '配信者IDには半角英数字のみ使用できます' | ||
}; | ||
return; | ||
} | ||
|
||
if (e instanceof SlugAlreadyUsedError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '配信者IDはすでに使われています' | ||
}; | ||
return; | ||
} | ||
|
||
if (e instanceof InviteAlreadyUsedError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '招待コードはすでに使われています' | ||
}; | ||
return; | ||
} | ||
|
||
if (e instanceof NoInviteError) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
errorCode: 'invalid_request', | ||
message: '招待コードが見つかりません' | ||
}; | ||
return; | ||
} | ||
|
||
throw e; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
fff26d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
knzklive2 – ./
knzklive2-git-production-knzklive.vercel.app
knzk.live
www.knzk.live
knzklive2-knzklive.vercel.app