Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/application/dtos/UpdateProfileDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { USER_KINDS } from '@/domain/entities/User'
export const UpdateProfileSchema = z.object({
eventId: z.string().regex(/^[a-z0-9]{7}$/),
userId: z.string().uuid(),
actorId: z.string().uuid(),
name: z.string().trim().min(2).max(50).optional(),
alias: z.string().trim().max(50).nullable().optional(),
email: z.string().trim().max(100).nullable().optional(),
Expand Down
9 changes: 7 additions & 2 deletions src/application/handlers/UpdateProfileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ export class UpdateProfileHandler {
const nextUsers = row.snapshot.users.map((u) =>
u.id === parsed.userId ? updated.toSnapshot() : u,
)
const actorName = row.snapshot.users.find((u) => u.id === parsed.actorId)?.name ?? 'Someone'
const description =
parsed.actorId === parsed.userId
? `${updated.displayName} updated their profile`
: `${actorName} updated ${updated.displayName}'s profile`
const nextSnapshot: EventSnapshot = HistoryAppender.append(
{ ...row.snapshot, users: nextUsers },
{
type: 'user_profile_updated',
userId: parsed.userId,
description: `${updated.displayName} updated their profile`,
userId: parsed.actorId,
description,
},
)

Expand Down
3 changes: 2 additions & 1 deletion src/presentation/components/features/event/ProfileEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function ProfileEditor({ userId, onClose }: { userId?: string; onClose: (

function save(e: FormEvent) {
e.preventDefault()
if (!event || !targetUserId) return
if (!event || !targetUserId || !me?.id) return
guardedExecute(async () => {
setBusy(true)
setError(null)
Expand All @@ -79,6 +79,7 @@ export function ProfileEditor({ userId, onClose }: { userId?: string; onClose: (
const result = await handler.execute({
eventId: event.id,
userId: targetUserId,
actorId: me.id,
name: name.trim(),
alias: alias.trim() || null,
email: email.trim() || null,
Expand Down
35 changes: 35 additions & 0 deletions tests/application/handlers/UpdateProfileHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('UpdateProfileHandler', () => {
new UpdateProfileHandler(repo).execute({
eventId: create.event.id,
userId: '018f4a8e-0000-7000-8000-000000000000',
actorId: create.creator.id,
email: 'x@y.com',
}),
).rejects.toThrow(/not in event/i)
Expand All @@ -22,6 +23,7 @@ describe('UpdateProfileHandler', () => {
const result = await new UpdateProfileHandler(repo).execute({
eventId: create.event.id,
userId: create.creator.id,
actorId: create.creator.id,
email: 'john@example.com',
allergies: [{ name: 'gluten', severity: 'severe' }],
})
Expand All @@ -39,10 +41,43 @@ describe('UpdateProfileHandler', () => {
const result = await new UpdateProfileHandler(repo).execute({
eventId: create.event.id,
userId: create.creator.id,
actorId: create.creator.id,
email: 'x@y.com',
})
const user = result.event.users.find((u) => u.id === create.creator.id)!
expect(user.alias).toBe('cousin')
expect(user.allergies).toEqual([])
})

it('records actor as the history userId and uses self description when editing own profile', async () => {
const repo = new InMemoryEventRepository()
const create = await new CreateEventHandler(repo).execute({ name: 'Trip', creatorName: 'John' })
const result = await new UpdateProfileHandler(repo).execute({
eventId: create.event.id,
userId: create.creator.id,
actorId: create.creator.id,
email: 'self@example.com',
})
const entry = result.event.history.at(-1)!
expect(entry.userId).toBe(create.creator.id)
expect(entry.description).toMatch(/updated their profile/i)
})

it('records actor as the history userId and credits the actor when editing someone elses profile', async () => {
const repo = new InMemoryEventRepository()
const create = await new CreateEventHandler(repo).execute({ name: 'Trip', creatorName: 'John' })
const joinHandler = new (await import('@/application/handlers/JoinAsNewUserHandler')).JoinAsNewUserHandler(repo)
const joined = await joinHandler.execute({ eventId: create.event.id, name: 'Maite' })
const result = await new UpdateProfileHandler(repo).execute({
eventId: create.event.id,
userId: joined.newUser.id,
actorId: create.creator.id,
email: 'edited@example.com',
})
const entry = result.event.history.at(-1)!
expect(entry.userId).toBe(create.creator.id)
expect(entry.description).toContain('John')
expect(entry.description).toContain("Maite")
expect(entry.description).not.toMatch(/their profile/i)
})
})