Skip to content

Conversation

@hib4
Copy link
Member

@hib4 hib4 commented Apr 19, 2025

PR Type

  • Enhancement
  • Bug fix

Description

  • Refactor auth registration using formatted user data

  • Add CRUD endpoints for ticket management

  • Introduce new ticket and updated user models

  • Update user routes and remove redundant controller file


Changes walkthrough 📝

Relevant files
Enhancement
8 files
auth_controller.ts
Refactor register to use formatUser for user data               
+12/-15 
ticket_controllers.ts
Add complete ticket CRUD endpoint implementations               
+126/-0 
user_controller.ts
Introduce endpoints for fetching current and all users     
+41/-0   
users_controller.ts
Remove duplicated user creation endpoints                               
+0/-101 
ticket.ts
Create ticket model interface and formatter function         
+21/-0   
user.ts
Create updated user model with default status                       
+31/-0   
ticket.ts
Add ticket routes secured with token validation                   
+21/-0   
user.ts
Update user routes; remove create endpoint route                 
+1/-6     
Configuration changes
1 files
index.ts
Update main router to include ticket routes                           
+3/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @hib4 hib4 self-assigned this Apr 19, 2025
    @github-actions
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Data Consistency

    The updated registration logic uses the formatUser function but omits several fields that were previously stored. Please verify that downstream processes relying on these fields are not negatively affected.

    const userData: User = formatUser({
      email: user.email ?? "",
      firstName: user.displayName ?? "",
      status: "not applicable",
    });
    
    await db
      .collection("users")
      .doc(user.uid)
      .set({
        ...userData,
        createdAt: FieldValue.serverTimestamp(),
      });
    Input Validation

    The new ticket CRUD endpoints appear to rely on minimal validation. Consider adding stricter input checks and ensuring that appropriate tests are in place to cover edge cases.

    import { Request, Response } from "express";
    import { db } from "../config/firebase";
    import { Ticket, formatTicket } from "../models/ticket";
    
    /**
     * Create a new ticket
     */
    export const createTicket = async (
      req: Request,
      res: Response
    ): Promise<void> => {
      try {
        const data = req.body as Partial<Ticket>;
    
        if (!req.user || !req.user.uid) {
          res.status(401).json({ error: "Unauthorized" });
          return;
        }
    
        const ticketRef = await db.collection("tickets").add({
          topic: data.topic || "",
          description: data.description || "",
          location: data.location || "",
          requestorId: req.user.uid,
          tags: Array.isArray(data.tags) ? data.tags : [],
          taken: false,
          resolved: false,
        });
    
        res.status(201).json({ success: true, id: ticketRef.id });
      } catch (error) {
        res.status(500).json({ error: (error as Error).message });
      }
    };
    
    /**
     * Get all tickets
     */
    export const getTickets = async (
      _req: Request,
      res: Response
    ): Promise<void> => {
      try {
        const snapshot = await db
          .collection("tickets")
          .where("resolved", "==", false)
          .get();
        const tickets = snapshot.docs.map((doc) =>
          formatTicket({ id: doc.id, ...doc.data() })
        );
        res.status(200).json(tickets);
      } catch (error) {
        res.status(500).json({ error: (error as Error).message });
      }
    };
    
    /**
     * Get a ticket by ID
     */
    export const getTicketById = async (
      req: Request,
      res: Response
    ): Promise<void> => {
      try {
        const { id } = req.params;
        const doc = await db.collection("tickets").doc(id).get();
    
        if (!doc.exists) {
          res.status(404).json({ error: "Ticket not found" });
          return;
        }
    
        res.status(200).json(formatTicket({ id: doc.id, ...doc.data() }));
      } catch (error) {
        res.status(500).json({ error: (error as Error).message });
      }
    };
    
    /**
     * Update a ticket
     */
    export const updateTicket = async (
      req: Request,
      res: Response
    ): Promise<void> => {
      try {
        const { id } = req.params;
        const data = req.body as Partial<Ticket>;
    
        const ticketDoc = db.collection("tickets").doc(id);
        const ticketSnap = await ticketDoc.get();
        if (!ticketSnap.exists) {
          res.status(404).json({ error: "Ticket not found" });
          return;
        }
        await ticketDoc.update(data);
    
        res.status(200).json({ success: true, message: "Ticket updated" });
      } catch (error) {
        res.status(500).json({ error: (error as Error).message });
      }
    };
    
    /**
     * Delete a ticket
     */
    export const deleteTicket = async (
      req: Request,
      res: Response
    ): Promise<void> => {
      try {
        const { id } = req.params;
    
        const ticketDoc = db.collection("tickets").doc(id);
        const ticketSnap = await ticketDoc.get();
        if (!ticketSnap.exists) {
          res.status(404).json({ error: "Ticket not found" });
          return;
        }
        await ticketDoc.delete();
    
        res.status(200).json({ success: true, message: "Ticket deleted" });
      } catch (error) {
        res.status(500).json({ error: (error as Error).message });
      }
    };

    @github-actions
    Copy link

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    @hib4 hib4 merged commit a6d54c1 into main Apr 19, 2025
    1 check passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants