I'm working on a project called `medics Communication Server`, which is a communication platform for different medics apps and patients. It allows them to communicate through various channels like text messages, emails, and WhatsApp. The server handles the sending of messages, logs all the details for future analysis, and provides a consolidated response to the client.

The project structure is as follows:

```
.
├── controllers
│   ├── configuration.controller.js
│   ├── index.js
│   └── messenger.controller.js
├── log
├── models
│   ├── configurations.model.js
│   ├── index.js
│   └── message_logs.model.js
├── public
├── services
│   ├── email
│   │   ├── GmailServiceProvider.js
│   │   └── OutlookServiceProvider.js
│   ├── sms
│   │   └── UnicelServiceProvider.js
│   ├── whatsapp
│   │   ├── GupshupServiceProvider.js
│   │   └── TelSpielServiceProvider.js
│   ├── HTTPService.js
│   ├── ServiceProviderFactory.js
│   └── logger.js
├── ReadMe
├── config.json
├── medics-communication-server.js
├── package-lock.json
├── package.json
└── routes.js
```

In this project, I have a service provider for WhatsApp called `TelSpielServiceProvider`. This service provider has a function `createTelSpielMessage` that creates a message payload based on the provided message details and configuration. However, different clients use different templates for the same template name, and these templates have different payloads.

Here's the current implementation of the `TelSpielServiceProvider`:

```javascript
const HttpService = require("../HTTPService"); // Update the path accordingly
const Logger = require('../logger.js');

class TelSpielServiceProvider {

  // Function to send a TelSpiel message
  async sendMessage(messageDetails, configuration) {
    try {
      // Create the TelSpiel message based on the provided details
      const message = this.createTelSpielMessage(messageDetails, configuration);

      // Log the message details before sending
      Logger.info(`Sending TelSpiel message to ${messageDetails.phone}: ${JSON.stringify(message)}`);

      // Send the message via TelSpiel API
      const response = await this.sendTelSpielMessage(message, configuration);

      // Log the response after sending
      Logger.info(`TelSpiel response for message to ${messageDetails.phone}: ${JSON.stringify(response)}`);

      return response;
    } catch (error) {
      console.error(error);
      Logger.error(`Error sending TelSpiel message: ${error.message}`);
      throw new Error(`Error sending TelSpiel message: ${error.message}`);
    }
  }

  // Function to send a TelSpiel message via the TelSpiel API
  async sendTelSpielMessage(message, configuration) {
    try {
      const url = configuration.url;

      // Use axios to make the POST request
      const response = await HttpService.post(url, message, {
        'Content-Type': 'application/json',
      });

      return response.data;
    } catch (error) {
      console.error(error);
      throw new Error(`Error sending TelSpiel message via API: ${error.message}`);
    }
  }

  // Function to create a TelSpiel message based on the provided message details
  createTelSpielMessage(messageDetails, configuration) {
    const { phone, content, attachment, attachmentURL, attachmentName } = messageDetails;

    // Define a generic message structure
    const message = {
      phoneNumber: phone,
      payload: {
        namespace: configuration.nameSpace, 
        language: {
          policy: 'deterministic',
          code: 'en',
        },
      },
    };

    if (attachment && attachment !== 'NULL') {
      // For media messages
      message.payload.components = [
        {
          type: 'header',
          parameters: [
            {
              type: 'video', // can be image, pdf or any document type 
              video: {
                link: attachmentURL,
              },
            },
          ],
        },
      ];
      message.payload.name = 'media_template';
    } else {
      // For text-based messages
      message.payload.components = [
        {
          type: 'body',
          parameters: [
            {
              type: 'text',
              text: content,
            },
          ],
        },
      ];
      message.payload.name = 'text_template';
    }

    return message;
  }

}

module.exports = TelSpielServiceProvider;
```

I want to make the `createTelSpielMessage` function more generic to handle different templates. The idea is to store these templates in a MongoDB collection and fetch the appropriate template based on the `hospitalCode`, `corporateEntityCode`, and `templateName` when creating a message. If a template is not found for a given `hospitalCode`, the function should try fetching by `corporateEntityCode`. If it's still not found, it should fall back to a default `hospitalCode` called 'PRODUCT'.

Could you please help me modify the `createTelSpielMessage` function to implement this functionality?"