Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to custom timezones in message history #36

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RapidProApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { VisitorMessageEndpoint } from './src/endpoint/VisitorMessageEndpoint';
import RapidProRestApi from './src/remote/rapidpro/RapidProRestApi';
import {
APP_SETTINGS,
CONFIG_DEFAULT_TIMEZONE,
CONFIG_HISTORY_TIME,
CONFIG_RAPIDPRO_AUTH_TOKEN,
CONFIG_REQUEST_TIMEOUT,
Expand Down Expand Up @@ -64,6 +65,7 @@ export class RapidProApp extends App implements IPostLivechatRoomClosed, IPostMe
const rpAuthToken = await read.getEnvironmentReader().getSettings().getValueById(CONFIG_RAPIDPRO_AUTH_TOKEN);
const reqTimeout = await read.getEnvironmentReader().getSettings().getValueById(CONFIG_REQUEST_TIMEOUT);
const historyTime = await read.getEnvironmentReader().getSettings().getValueById(CONFIG_HISTORY_TIME);
const defaultTimezone = await read.getEnvironmentReader().getSettings().getValueById(CONFIG_DEFAULT_TIMEZONE)

const after = new Date();
after.setHours(after.getHours() - historyTime);
Expand All @@ -77,6 +79,7 @@ export class RapidProApp extends App implements IPostLivechatRoomClosed, IPostMe
const messages = await rapidProDataSource.getMessages(
job.contactUUID,
after.toISOString(),
defaultTimezone,
);

const livechatRepo = new LiveChatRepositoryImpl(
Expand Down
2 changes: 1 addition & 1 deletion src/data/rapidpro/IRapidProRemoteDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import RPMessage from '../../domain/RPMessage';

export default interface IRapidProRemoteDataSource {

getMessages(contactUUID: string, after: string): Promise<Array<RPMessage>>;
getMessages(contactUUID: string, after: string, defaultTimezone: number): Promise<Array<RPMessage>>;

startFlow(uuid: string, visitor: IVisitor, extra: any): Promise<void>;

Expand Down
5 changes: 3 additions & 2 deletions src/remote/rapidpro/RapidProRestApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ export default class RapidProRestApi implements IRapidProRemoteDataSource {
this.timeout = this.timeout < 5 ? 5 : this.timeout;
}

public async getMessages(contactUUID: string, after: string): Promise<Array<RPMessage>> {
public async getMessages(contactUUID: string, after: string, defaultTimezone: number): Promise<Array<RPMessage>> {
const reqOptions = this.requestOptions();
reqOptions['params'] = { contact: contactUUID, after };

const response = await this.http.get(this.baseUrl + '/api/v2/messages.json', reqOptions);
if (!response || response.statusCode !== HttpStatusCode.OK) {
return [];
}
const tzOffset = DateStringUtils.getTimezoneOffsetInMinutes(after);

const tzOffset = defaultTimezone ? defaultTimezone * 60 : DateStringUtils.getTimezoneOffsetInMinutes(after);

const result: Array<RPMessage> = [];

Expand Down
9 changes: 9 additions & 0 deletions src/settings/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const CONFIG_APP_SECRET = 'config_app_secret';
export const CONFIG_RAPIDPRO_AUTH_TOKEN = 'config_rapidpro_auth_token';
export const CONFIG_REQUEST_TIMEOUT = 'config_request_timeout';
export const CONFIG_HISTORY_TIME = 'config_history_time';
export const CONFIG_DEFAULT_TIMEZONE = 'config_default_timezone';

export const APP_SETTINGS: Array<ISetting> = [
{
Expand Down Expand Up @@ -40,4 +41,12 @@ export const APP_SETTINGS: Array<ISetting> = [
public: false,
i18nLabel: CONFIG_HISTORY_TIME,
},
{
id: CONFIG_DEFAULT_TIMEZONE,
type: SettingType.NUMBER,
packageValue: null,
required: false,
public: false,
i18nLabel: CONFIG_DEFAULT_TIMEZONE,
},
];