From e3b75ce0f1615d1e047655868e2cdea62b9120b5 Mon Sep 17 00:00:00 2001 From: Kajal Thakur Date: Thu, 27 Mar 2025 06:57:01 +0000 Subject: [PATCH] code update recorded at: 27/03/25 06:57:01 --- handler.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 handler.js diff --git a/handler.js b/handler.js new file mode 100644 index 0000000..b2c5506 --- /dev/null +++ b/handler.js @@ -0,0 +1,99 @@ +// BOILER_CODE_START + +import crypto from 'crypto' + +class EventNotSupportedException extends Error { + constructor(message) { + super(message); + this.name = 'EventNotSupportedException'; + this.statusCode = 451; + this.message = message; + } +} + +class InvalidEventPayloadException extends Error { + constructor(message) { + super(message); + this.statusCode = 452; + this.name = 'InvalidEventPayloadException'; + this.message = message; + } +} + +class RetryErrorException extends Error { + constructor(message) { + super(message); + this.statusCode = 453; + this.name = 'RetryErrorException'; + this.message = message; + } +} + +function generateSha256Hash(input) { + return crypto.createHash('sha256').update(input).digest('hex'); +} + +export const handler = async (req, res) => { + try { + const obj = req.body; + modifyObj(obj); + res.setHeader('Content-Type', 'application/json'); + res.send(JSON.stringify(obj)); + } catch (error) { + console.error(error); + const statusCode = error.statusCode || 500; + res.statusCode = statusCode; + res.setHeader('Content-Type', 'application/json'); + res.send(JSON.stringify(statusCode === 500 + ? { message: error.message } + : { name: error.name, message: error.message, statusCode })); + } +}; + +// BOILER_CODE_END + +function modifyObj(obj) { + // Check if 'properties.index' exists + if (obj.properties?.index) { + // Add transformation status + obj.properties.transformationStatus = "success"; + + // Generate SHA-256 hash of 'properties.index' + obj.properties.hashValue = generateSha256Hash(obj.properties.index); + + // Remove the 'properties.index' key + delete obj.properties.index; + + // Indicate deletion status + obj.properties.deleteStatus = "success"; + } else { + // Throw an exception if 'properties.index' is missing + throw new InvalidEventPayloadException("'properties.index' is required but missing..."); + } + + return obj; +} + +/* +Exceptions available: + throw new EventNotSupportedException('your message here'); + throw new InvalidEventPayloadException('your message here'); + throw new RetryErrorException('your message here'); + +Method for generating 256 hash code for a String: + let hashOfAString = generateSha256Hash(input); + +*/ + + +/* +Exceptions available: + throw new EventNotSupportedException('your message here'); + throw new InvalidEventPayloadException('your message here'); + throw new RetryErrorException('your message here'); + +Method for generating 256 hash code for a String: + let hashOfAString = generateSha256Hash(input); + +*/ +