FPT-Capstone-Group / Smart_Parking_Server

Server to Manage bike's ownership physical card, application and parking session
0 stars 0 forks source link

Return parking History by plateNumber. #28

Open trongtomo opened 11 months ago

trongtomo commented 11 months ago
const getParkingSessionsByPlateNumber = async (req, res) => {
  try {
    const { plateNumber } = req.params;
    const user = req.user;
    // Bike already associate owner when updateRegis, only check bike associate with user through card
    const bike = await Bike.findOne({
      where: { plateNumber, userId: user.userId },
      include: [
        {
          model: Card,
          required: true,
          where: { bikeId: Sequelize.col("Bike.bikeId"), userId: user.userId },
        },
      ],
    });

    if (!bike) {
      return errorResponse(
        req,
        res,
        "User does not own the bike or is not associated with it through a card",
        404
      );
    }

    // If the user is associated, fetch the parking sessions
    const parkingSessions = await ParkingSession.findAll({
      where: { plateNumber },
    });

    if (!parkingSessions || parkingSessions.length === 0) {
      return errorResponse(
        req,
        res,
        "No parking sessions found for the bike",
        404
      );
    }

    // Format the response or perform additional operations if needed
    const formattedParkingSessions = parkingSessions.map((session) =>
      formatParkingSession(session)
    );

    return successResponse(req, res, formattedParkingSessions, 200);
  } catch (error) {
    console.error("Internal Server Error:", error);
    return errorResponse(req, res, "Internal Server Error", 500, error);
  }
};
trongtomo commented 11 months ago

Only check bike associate with user through card, since bike already associate with owner in updated Registration, so in parking History we can see all bike history from different owner too ( if they associated with that bike)