Skip to content

Conversation

Copy link

Copilot AI commented Oct 16, 2025

Overview

Implements the PATCH /users/{userId} endpoint for updating user information in the Users table, addressing issue requirements for partial updates with database integration.

Changes Made

1. Users Lambda Handler

Created a new Lambda handler at apps/backend/lambdas/users/ using the lambda-cli scaffolding tool. This provides:

  • Full TypeScript support with proper typing
  • Health check endpoint at /health
  • Local development server with hot-reload support
  • OpenAPI specification for API documentation

2. Database Integration with Kysely

  • Installed and configured Kysely ORM with better-sqlite3 driver
  • Created database.ts with type-safe database schema definitions
  • Implemented helper functions to convert between SQLite integers and JavaScript booleans
  • Database connection management with environment variable support

3. Database Schema Updates

Extended the existing user table with two new columns:

  • name (VARCHAR, nullable) - stores user's display name
  • isAdmin (INTEGER, default 0) - stores admin status (0 = false, 1 = true)

Migration SQL is documented in apps/backend/lambdas/users/migration.sql for reproducibility.

4. PATCH /users/{userId} Implementation

Implemented comprehensive partial update logic:

// Update both fields
PATCH /users/1
{ "name": "John Doe", "isAdmin": true }

// Update only name (partial update)
PATCH /users/1
{ "name": "Jane Smith" }

// Update only isAdmin (partial update)
PATCH /users/1
{ "isAdmin": false }

Features:

  • ✅ Partial updates - any combination of fields
  • ✅ Type validation - ensures name is string, isAdmin is boolean
  • ✅ ID validation - validates userId is a valid integer
  • ✅ Existence check - returns 404 for non-existent users
  • ✅ Required fields - at least one field must be provided
  • ✅ Returns updated user data with proper boolean conversion

Response Example:

{
  "id": 1,
  "email": "user@example.com",
  "name": "John Doe",
  "isAdmin": true
}

5. Comprehensive Testing

Added 11 Jest test cases covering all scenarios:

  • Successful updates (name only, isAdmin only, both fields)
  • Partial update behavior validation
  • Error handling (404 for missing users, 400 for invalid inputs)
  • Type validation for all fields
  • Edge cases (empty body, invalid ID format)

All tests passing: ✅ 11/11

6. Documentation

  • Created comprehensive README.md with API documentation
  • Documented setup instructions and local development workflow
  • Added example curl commands for testing
  • Documented database schema and migration steps

API Documentation

Endpoint

PATCH /users/{userId}

Request

curl -X PATCH http://localhost:3000/users/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe", "isAdmin": true}'

Responses

  • 200 OK - User updated successfully with user object
  • 400 Bad Request - Invalid userId format, invalid field types, or no fields provided
  • 404 Not Found - User does not exist
  • 500 Internal Server Error - Server error

Testing

Manual testing verified with local dev server and automated tests confirm all acceptance criteria:

  • ✅ Route updates user information correctly
  • ✅ Returns 200 with updated user data
  • ✅ Jest tests confirm updates, invalid IDs, and invalid fields
  • ✅ Partial updates work as expected

Technical Details

  • Framework: AWS Lambda with TypeScript
  • Database: SQLite with Kysely ORM
  • Testing: Jest with ts-jest
  • Build: TypeScript compiler
  • Local Dev: Custom dev-server with hot-reload

Files Changed

  • Created apps/backend/lambdas/users/ directory with complete Lambda handler
  • Updated db.sqlite with schema changes
  • Added migration script, tests, and documentation
Original prompt

This section details on the original issue you should resolve

<issue_title>[Users] Add Update User Route (PATCH /users/{userId})</issue_title>
<issue_description>## User Story
As a user I want to update user information in the Users table.

Description

  • Add the PATCH /users/{userId} route to the users Lambda using:
    node tools/lambda-cli.js add-route users PATCH /users/{userId} --body name:string,isAdmin:boolean
  • Implement partial update logic using Kysely (db.updateTable('users').set({ name, isAdmin }).where('userID', '=', userId)).
  • Test using Swagger and Jest.

Acceptance Criteria

  • Route updates user information correctly.
  • Swagger returns 200 with updated user data.
  • Jest tests confirm updates, invalid IDs, and invalid fields.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #51


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

- Created users Lambda handler using lambda-cli
- Installed Kysely and better-sqlite3 for database operations
- Added name and isAdmin columns to user table
- Implemented PATCH /users/{userId} route with partial update support
- Added comprehensive Jest tests (11 test cases)
- All tests passing
- Manual testing verified with curl and dev server

Co-authored-by: denniwang <66754085+denniwang@users.noreply.github.com>
Copilot AI changed the title [WIP] Add PATCH route for updating user information Add PATCH /users/{userId} route with Kysely database integration Oct 16, 2025
Copilot AI requested a review from denniwang October 16, 2025 22:45
Copilot finished work on behalf of denniwang October 16, 2025 22:45
@denniwang denniwang mentioned this pull request Oct 23, 2025
@denniwang denniwang closed this Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Users] Add Update User Route (PATCH /users/{userId})

2 participants