Issue with timezone in WSD module

Akshaya14
Tera Contributor

Hi team,

In WSD module we created one custom action for recurring reservation after creating the reservation user can select the date range to cancel the particular reservation from this date range. so created one "Multiday cancel reservation" once they click this one pop up will show to enter the date range start and end date (note: only date). In our script with addressed the date format issue from different user we convert this to default (YYYY-MM-DD) but issue with timezone. If user selected the start date as 04/11/2024 and end date is 04/11/2024  in timezone of Asia/magadan and in reservation was made in US/Central then the reservation in portal 03/11/2024 cancelling but in backend the same reservation shows as 04/11/2024. Please help me to correct this .

@Tony Chatfield1 @Manikmodi16c@robb

@Robbie 
var WSDRestBulkCancellation = Class.create();
WSDRestBulkCancellation.prototype = {
initialize: function() {},

process: function(request, response) {
var RESOURCE_PATH = '/api/sn_wsd_rsv/wsd_bulk_cancellation/update';
var apiHelper = new WSDApiHelper();
var reservationValidator = new WSDReservationValidator();
var aclVerificationService = new WSDAclVerificationService();

var pathParams = request.pathParams;
var reservationId = pathParams.sys_id;
var requestData = apiHelper.getRequestDataJson(request);

try {
if (!reservationId || typeof reservationId !== 'string') {
var badRequestUserMsg = gs.getMessage('Invalid request');
apiHelper.setBadRequestResponse(
response,
RESOURCE_PATH,
badRequestUserMsg,
WSDUtils.formatString('Invalid or no reservation sys_id provided: {0}.', reservationId),
requestData
);
return;
}

var gr = new GlideRecord('sn_wsd_rsv_reservation');
gr.addQuery('source_reservation', reservationId);
gr.query();
if (!gr.hasNext()) {
response.setError(new sn_ws_err.NotFoundError('Records Not Found'));
response.setStatus(404);
return;
}

var answer = {
success: false
};

var requestBody = request.body.data;
//below 2 lines was added for the story STRY1479578 - Multi-Day Cancellation Defect
var s = this.convertToUserDateFormat(requestBody.start); // Convert start date
var e = this.convertToUserDateFormat(requestBody.end);


if (!s && !e) {
response.setError(new sn_ws_err.NotFoundError('Request data not provided'));
response.setStatus(404);
return;
}

var gr = new GlideRecord('sn_wsd_rsv_reservation');
gr.addQuery('source_reservation', reservationId);
gr.query();
if (!gr.hasNext()) {
response.setError(new sn_ws_err.NotFoundError('Records Not Found'));
response.setStatus(404);
return;
}

while (gr.next()) {
//below line was updated for the story STRY1479578 - Multi-Day Cancellation Defect
var start_date = this.convertToUserDateFormat(gr.getDisplayValue('start').split(' ')[0]);
if (start_date >= s && start_date <= e) {
gr.state = 'cancelled';
gr.active = false; // Enable bulk reservation
gr.update();
answer.success = true;
}
}

response.setBody(answer);
} catch (ex) {
var exceptionOccurredMsg = gs.getMessage('Error occurred! Unable to cancel reservations');
apiHelper.setResponse(response, 500, RESOURCE_PATH, exceptionOccurredMsg, ex, requestData);
}
},
//below lines 82-99 was added for the story STRY1479578 - Multi-Day Cancellation Defect
convertToUserDateFormat: function(dateTimeString, userId) {
// Get the user's date format using the pre-existing function from WSDUtils
var userDateFormat = WSDUtils.getUserDateAndTimeFormat(userId).dateFormat;

// If no date string is provided, return null
if (!dateTimeString) return null;

// Try parsing the date using Moment.js and the user's format
var userDate = moment(dateTimeString, userDateFormat, true);

// If the date is valid, format it as 'YYYY-MM-DD'
if (userDate.isValid()) {
return userDate.format('YYYY-MM-DD');
} else {
gs.error('Invalid date format: ' + dateTimeString);
return null; // Return null if the date cannot be parsed
}
},

type: 'WSDRestBulkCancellation'
};

0 REPLIES 0