Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Apr 10, 2024
1 parent 7f0b620 commit 2a67da8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/simple-examples/src/conversation/messages/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { getAppIdFromConfig, getContactIdFromConfig, initConversationService, pr
queue: 'HIGH_PRIORITY',
processing_strategy: 'DEFAULT',
channel_priority_order: ['MESSENGER'],
ttl_seconds: 60,
ttl: 60,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ConversationEventService {
contact_id: message.contact_id!,
},
message: this.buildContactMessage(contactMessage),
ttl_seconds: 5,
ttl: 5,
channel_properties: {
MESSENGER_NOTIFICATION_TYPE: 'NO_PUSH',
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface SendMessageRequestBase<T extends Recipient> {
/** @see Recipient */
recipient: T;
/** The timeout allotted for sending the message, expressed in seconds. Passed to channels which support it and emulated by the Conversation API for channels without ttl support but with message retract/unsend functionality. Channel failover will not be performed for messages with an expired TTL. The format is an integer with the suffix `s` (for seconds). Valid integer range is 3 to 315,576,000,000 (inclusive). Example values include `10s` (10 seconds) and `86400s` (24 hours). */
ttl_seconds?: number;
ttl?: string | number;
/** Overrides the app\'s [Processing Mode](../../../../../conversation/processing-modes/). Default value is `DEFAULT`. */
processing_strategy?: ProcessingStrategy;
/** An arbitrary identifier that will be propagated to callbacks related to this message, including MO replies. Only applicable to messages sent with the `CONVERSATION` processing mode. Up to 128 characters long. */
Expand Down
25 changes: 18 additions & 7 deletions packages/conversation/src/rest/v1/messages/messages-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,11 @@ export class MessagesApi extends ConversationDomainApi {
'Accept': 'application/json',
};

// Special fields handling: ttl_seconds: number => ttl: string
if (data.sendMessageRequestBody.ttl_seconds !== undefined) {
(data as any).sendMessageRequestBody.ttl = data.sendMessageRequestBody.ttl_seconds + 's';
delete data.sendMessageRequestBody.ttl_seconds;
}
// Special fields handling: see method for details
const requestDataBody = this.performSendMessageRequestBodyTransformation(data.sendMessageRequestBody);

const body: RequestBody = data['sendMessageRequestBody']
? JSON.stringify(data['sendMessageRequestBody'])
const body: RequestBody = requestDataBody
? JSON.stringify(requestDataBody)
: '{}';
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/messages:send`;

Expand All @@ -383,6 +380,20 @@ export class MessagesApi extends ConversationDomainApi {
});
}

performSendMessageRequestBodyTransformation(
body: SendMessageRequest<Recipient>

Check warning on line 384 in packages/conversation/src/rest/v1/messages/messages-api.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing trailing comma

Check warning on line 384 in packages/conversation/src/rest/v1/messages/messages-api.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing trailing comma
): SendMessageRequest<Recipient> {
const requestDataBody = { ...body };
// 'ttl' field can be a number or a string and needs to be formatted as for instance "10s" to be accepted by the server
if (typeof requestDataBody.ttl === 'number') {
requestDataBody.ttl = requestDataBody.ttl.toString();
}
if (typeof requestDataBody.ttl === 'string' && !requestDataBody.ttl.endsWith('s')) {
requestDataBody.ttl = requestDataBody.ttl + 's';
}
return requestDataBody;
}

/**
* Update message metadata
* Update a specific message metadata by its ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ describe('MessagesApi', () => {
message: {
...messageBuilder.text(textMessageItem),
},
ttl_seconds: 20,
};
const requestDataWithContactId: SendMessageRequestData<ContactId> = {
sendMessageRequestBody: {
Expand Down Expand Up @@ -226,6 +225,22 @@ describe('MessagesApi', () => {
expect(response).toEqual(expectedResponse);
expect(fixture.send).toHaveBeenCalledWith(requestData);
});

it('should format the ttl field', () => {
const requestBody: SendMessageRequest<Recipient> = {
...sendMessageRequest,
...recipientContactId,
};
requestBody.ttl = 20;
let formattedBody = messagesApi.performSendMessageRequestBodyTransformation(requestBody);
expect(formattedBody.ttl).toBe('20s');
requestBody.ttl = '20';
formattedBody = messagesApi.performSendMessageRequestBodyTransformation(requestBody);
expect(formattedBody.ttl).toBe('20s');
requestBody.ttl = '20s';
formattedBody = messagesApi.performSendMessageRequestBodyTransformation(requestBody);
expect(formattedBody.ttl).toBe('20s');
});
});

describe ('sendCardMessage', () => {
Expand Down

0 comments on commit 2a67da8

Please sign in to comment.