kjsdb

sdf
mail@pastecode.io avatar
unknown
plain_text
a month ago
2.5 kB
1
Indexable
Never
async function checkEventClash(conn, eventInfoRows,eventCode) {

	  // Construct the placeholders for the event codes
	  // Construct the placeholders for the event codes
	const placeholders = eventInfoRows.map(event => `'${event.EventCode}'`).join(', ');

	  console.log(eventCode);
	  // Construct the SQL query with placeholders for the event codes
	  const query = `
		  SELECT *
		  FROM Events
		  WHERE EventCode IN (${placeholders})
		  AND (
			StartTime BETWEEN
			  SUBTIME((SELECT StartTime FROM Events WHERE EventCode = '${eventCode}'), '00:30:00') AND
			  ADDTIME((SELECT StartTime FROM Events WHERE EventCode = '${eventCode}'), '00:30:00')
		  );
	  `
	console.log(query);
	  // Combine the event codes with the start and end times
	  const params = [...placeholders];
  
	  const [rows, fields] = await conn.execute(query, params);
	  console.log(rows);
	  return rows;


}
async function checkParticipantEventClashes(conn, eventCode, participantID) {
	// const [participantEventsRows, participantEventsFields] = await conn.execute(
	// 	`SELECT EventCode FROM SoloRegistration WHERE ParticipantID = '${participantID}'`
	// );
	const [eventInfoRows, eventInfoFields] = await conn.execute(
		`SELECT e.*
		FROM Events e
		JOIN (
			SELECT EventCode
			FROM SoloRegistration
			WHERE ParticipantID = '${participantID}'
			UNION
			SELECT EventCode
			FROM TeamRegistration
			WHERE ParticipantID = '${participantID}'
		) AS r ON e.EventCode = r.EventCode
		WHERE e.EventCode != '${eventCode}';
		`
	);
	// Check for clashes with each event code

			// Assuming your checkEventClash function is correctly implemented
			const isClash = await checkEventClash(
				conn,
				eventInfoRows,
				eventCode,

			);
			
			
			// if (isClash) {
			// 	return {
			// 		code: 200,
			// 		message: `Event Clash! Clashing with event: ${rows}`,
			// 		type: "Info",
			// 		eventCodeClashName: rows,
			// 	};
			// }
			if (isClash.length>0) {
				// Extract EventNames from the rows array
				
				const clashingEventNames = isClash.map(row => row.EventName).join(', ');
			
				return {
					code: 200,
					message: `Event Clash! Clashing with events: ${clashingEventNames}`,
					type: "Info",
					eventCodeClashName: clashingEventNames,
				};
			}

		
	// If no clashes found with any event, return success message
	return {
		code: 200,
		message: `No clashes found with any event`,
		type: "Success",
	};


}
Leave a Comment