-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from octo-technology/track-progress
Track progress
- Loading branch information
Showing
51 changed files
with
776 additions
and
1,004 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
File renamed without changes.
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
46 changes: 46 additions & 0 deletions
46
src/api/src/resolvers/user/addProgress/addProgress.spec.ts
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,46 @@ | ||
import { Context, Next } from 'koa' | ||
|
||
import { Jwt } from '../../../shared/user/Jwt' | ||
import { User } from '../../../shared/user/User' | ||
import { createTestUser } from '../../../test/createTestUser' | ||
import { deleteTestUser } from '../../../test/deleteTestUser' | ||
import { mockConnect } from '../../../test/mockConnect' | ||
import { addProgress } from './addProgress' | ||
|
||
let user: User | ||
let next: Next | ||
let jwt: Jwt | ||
|
||
describe('User', () => { | ||
beforeAll(async () => { | ||
await mockConnect() | ||
const created = await createTestUser('[email protected]', 'bob', 'Bob1234#') | ||
user = created.user | ||
jwt = created.jwt | ||
next = created.next | ||
}) | ||
|
||
it('can add progress', async (done) => { | ||
const ctx: Context = { | ||
request: { | ||
headers: { | ||
authorization: 'Bearer ' + jwt, | ||
}, | ||
body: { | ||
chapterDone: '/pascal/chapter-polymorphism', | ||
}, | ||
}, | ||
} as Context | ||
|
||
await addProgress(ctx, next) | ||
|
||
expect(ctx.body.user).toBeDefined() | ||
expect(ctx.body.user.progress).toContain('/pascal/chapter-polymorphism') | ||
|
||
done() | ||
}) | ||
|
||
afterAll(async () => { | ||
await deleteTestUser(user._id) | ||
}) | ||
}) |
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,41 @@ | ||
import { plainToClass } from 'class-transformer' | ||
import { validateOrReject } from 'class-validator' | ||
import { Context, Next } from 'koa' | ||
|
||
import { firstError } from '../../../helpers/firstError' | ||
import { toPublicUser } from '../../../helpers/toPublicUser' | ||
import { AddProgressInputs, AddProgressOutputs } from '../../../shared/user/AddProgress' | ||
import { PublicUser } from '../../../shared/user/PublicUser' | ||
import { User, UserModel } from '../../../shared/user/User' | ||
import { rateLimit } from '../../quota/rateLimit/rateLimit' | ||
import { authenticate } from '../helpers/authenticate' | ||
|
||
export const PUBLIC_USER_MONGO_SELECTOR = '_id username emailVerified createdAt' | ||
|
||
export const addProgress = async (ctx: Context, next: Next): Promise<void> => { | ||
const addProgressArgs = plainToClass(AddProgressInputs, ctx.request.body, { excludeExtraneousValues: true }) | ||
await validateOrReject(addProgressArgs, { forbidUnknownValues: true }).catch(firstError) | ||
const { chapterDone } = addProgressArgs | ||
|
||
const user: User = await authenticate(ctx) | ||
|
||
await rateLimit(user._id) | ||
|
||
await UserModel.updateOne( | ||
{ _id: user._id }, | ||
{ $addToSet: { progress: chapterDone } }, | ||
).exec() | ||
|
||
const updatedUser: User = await UserModel.findOne( | ||
{ _id: user._id }, | ||
).lean() as User | ||
|
||
const publicUser: PublicUser = toPublicUser(updatedUser) | ||
|
||
const response: AddProgressOutputs = { user: publicUser } | ||
|
||
ctx.status = 200 | ||
ctx.body = response | ||
|
||
await next() | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/api/src/shared/user/GetPublicUser.ts → src/api/src/shared/page/GetPublicUser.ts
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,15 @@ | ||
import { Expose } from 'class-transformer' | ||
import { Length, Matches } from 'class-validator' | ||
|
||
import { PublicUser } from '../user/PublicUser' | ||
|
||
export class AddProgressInputs { | ||
@Expose() | ||
@Length(2, 100) | ||
@Matches(/^[a-zA-Z0-9-\/]*$/, { message: 'Chapter slug can only contain letters, numbers, dashes and slashes' }) | ||
chapterDone!: string | ||
} | ||
|
||
export class AddProgressOutputs { | ||
user!: PublicUser | ||
} |
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.