Skip to content
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
39 changes: 39 additions & 0 deletions src/apis/html-page-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,45 @@ class HTMLPageAPI {
return this._sendDelete(url, data);
}

sendNotification(page_id, notification_data, preview_table_config) {
const url = `${this.server}api/v2.1/universal-apps/${this.appUuid}/notification/`;
const data = { ...notification_data, page_id };
if (preview_table_config && typeof preview_table_config === 'object') {
data.preview_table_config = preview_table_config;
}
return this.req.post(url, data);
}

sendEmail(page_id, email_data, preview_table_config) {
const url = `${this.server}api/v2.1/universal-apps/${this.appUuid}/email/`;
const data = { ...email_data, page_id };
if (preview_table_config && typeof preview_table_config === 'object') {
data.preview_table_config = preview_table_config;
}
return this.req.post(url, data);
}

getMessageStatus(task_id) {
const url = `${this.server}api/v2.1/universal-apps/${this.appUuid}/dtable-message-status/`;
return this.req.get(url, { params: { task_id } });
}

runScript(script_name, page_id, table_name, row_id, preview_table_config) {
const encodedScriptName = encodeURIComponent(script_name);
const url = `${this.server}api/v2.1/universal-apps/${this.appUuid}/run-script/${encodedScriptName}/`;
const data = { page_id, table_name, row_id };
if (preview_table_config && typeof preview_table_config === 'object') {
data.preview_table_config = preview_table_config;
}
return this.req.post(url, data);
}

getScriptResult(script_name, task_id, page_id) {
const encodedScriptName = encodeURIComponent(script_name);
const url = `${this.server}api/v2.1/universal-apps/${this.appUuid}/run-script/${encodedScriptName}/result/${task_id}/`;
return this.req.get(url, { params: { page_id } });
}

getUploadLink(page_id, upload_type = 'file') {
const url = `${this.server}api/v2.1/universal-apps/${this.appUuid}/html-page-upload-link/`;
return this.req.get(url, { params: { page_id, upload_type } });
Expand Down
59 changes: 59 additions & 0 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,65 @@ export class HTMLPageSDK {
return this.htmlPageAPI.deleteLinks(this.options.pageId, tableName, linksData, previewTableConfig);
}

sendNotification({ tableName, rowId, emails, msg }) {
const previewTableConfig = this._getPreviewTableConfig({ tableName });
return this.htmlPageAPI.sendNotification(
this.options.pageId,
{ table_name: tableName, row_id: rowId, emails, msg },
previewTableConfig,
);
}

sendEmail({
tableName,
rowId,
accountName,
sendTo,
copyTo,
replyTo,
subject,
message,
htmlMessage,
attachmentColumnNames,
}) {
const previewTableConfig = this._getPreviewTableConfig({ tableName });
return this.htmlPageAPI.sendEmail(
this.options.pageId,
{
table_name: tableName,
row_id: rowId,
account_name: accountName,
send_to: sendTo,
copy_to: copyTo,
reply_to: replyTo,
subject,
message,
html_message: htmlMessage,
attachment_column_names: attachmentColumnNames,
},
previewTableConfig,
);
}

getMessageStatus({ taskId }) {
return this.htmlPageAPI.getMessageStatus(taskId);
}

runScript({ scriptName, tableName, rowId }) {
const previewTableConfig = this._getPreviewTableConfig({ tableName });
return this.htmlPageAPI.runScript(
scriptName,
this.options.pageId,
tableName,
rowId,
previewTableConfig,
);
}

getScriptResult({ scriptName, taskId }) {
return this.htmlPageAPI.getScriptResult(scriptName, taskId, this.options.pageId);
}

uploadFile({ file }) {
return this.htmlPageAPI.upload(this.options.pageId, file);
}
Expand Down
173 changes: 173 additions & 0 deletions tests/api-action.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import axios from 'axios';
import HTMLPageAPI from '../src/apis/html-page-api';

jest.mock('axios', () => ({
__esModule: true,
default: {
create: jest.fn(),
get: jest.fn(),
post: jest.fn(),
},
}));

function createApi() {
const get = jest.fn();
const post = jest.fn();
axios.create.mockReturnValue({ get, post });

const api = new HTMLPageAPI();
api.init({
server: 'https://example.com/',
accessToken: 'token',
appUuid: 'app-uuid',
});

return { api, get, post };
}

describe('HTMLPageAPI action operations', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('sends notification and email requests with public action parameters', () => {
const { api, post } = createApi();
const notificationResponse = { data: { task_id: 'notification-task-1' } };
const emailResponse = { data: { task_id: 'email-task-1' } };
post.mockReturnValueOnce(notificationResponse).mockReturnValueOnce(emailResponse);

const notificationResult = api.sendNotification('page-1', {
table_name: 'Products',
row_id: 'row-1',
emails: ['recipient@example.com'],
msg: 'hello {Name}',
});
const emailResult = api.sendEmail('page-1', {
table_name: 'Products',
row_id: 'row-1',
account_name: 'Operations SMTP',
send_to: ['recipient@example.com'],
copy_to: ['audit@example.com'],
reply_to: ['service@example.com'],
subject: 'Order {Order number}',
html_message: '<p>Hello</p>',
attachment_column_names: ['Attachment'],
});

expect(notificationResult).toBe(notificationResponse);
expect(emailResult).toBe(emailResponse);
expect(post).toHaveBeenNthCalledWith(
1,
'https://example.com/api/v2.1/universal-apps/app-uuid/notification/',
{
page_id: 'page-1',
table_name: 'Products',
row_id: 'row-1',
emails: ['recipient@example.com'],
msg: 'hello {Name}',
},
);
expect(post).toHaveBeenNthCalledWith(
2,
'https://example.com/api/v2.1/universal-apps/app-uuid/email/',
{
page_id: 'page-1',
table_name: 'Products',
row_id: 'row-1',
account_name: 'Operations SMTP',
send_to: ['recipient@example.com'],
copy_to: ['audit@example.com'],
reply_to: ['service@example.com'],
subject: 'Order {Order number}',
html_message: '<p>Hello</p>',
attachment_column_names: ['Attachment'],
},
);
});

it('sends preview_table_config with table-scoped action requests', () => {
const { api, post } = createApi();
const previewTableConfig = { table_id: 'table-1', permissions: {} };

api.sendNotification('ai_agent', {
table_name: 'Products',
row_id: 'row-1',
emails: ['recipient@example.com'],
msg: 'hello',
}, previewTableConfig);
api.sendEmail('ai_agent', {
table_name: 'Products',
row_id: 'row-1',
account_name: 'Operations SMTP',
send_to: ['recipient@example.com'],
subject: 'Hello',
message: 'Hello',
}, previewTableConfig);
api.runScript('Generate report', 'ai_agent', 'Products', 'row-1', previewTableConfig);

expect(post).toHaveBeenNthCalledWith(1, expect.any(String), expect.objectContaining({
page_id: 'ai_agent',
preview_table_config: previewTableConfig,
}));
expect(post).toHaveBeenNthCalledWith(2, expect.any(String), expect.objectContaining({
page_id: 'ai_agent',
preview_table_config: previewTableConfig,
}));
expect(post).toHaveBeenNthCalledWith(3, expect.any(String), expect.objectContaining({
page_id: 'ai_agent',
preview_table_config: previewTableConfig,
}));
});

it('gets message status by task ID', () => {
const { api, get } = createApi();
const response = { data: { status: 'SUCCESS' } };
get.mockReturnValue(response);

const result = api.getMessageStatus('message-task-1');

expect(result).toBe(response);
expect(get).toHaveBeenCalledWith(
'https://example.com/api/v2.1/universal-apps/app-uuid/dtable-message-status/',
{ params: { task_id: 'message-task-1' } },
);
});

it('runs and gets a script by its display name', () => {
const { api, get, post } = createApi();
const runResponse = { data: { task_id: 42 } };
const resultResponse = {
data: {
script: {
state: 'finished',
success: false,
output: 'Script execution failed',
return_code: 125,
},
},
};
post.mockReturnValue(runResponse);
get.mockReturnValue(resultResponse);

const runResult = api.runScript('Generate / report', 'page-1');
const result = api.getScriptResult('Generate / report', 42, 'page-1');

expect(runResult).toBe(runResponse);
expect(result).toBe(resultResponse);
expect(result.data.script).toEqual({
state: 'finished',
success: false,
output: 'Script execution failed',
return_code: 125,
});
expect(post).toHaveBeenCalledWith(
'https://example.com/api/v2.1/universal-apps/app-uuid/run-script/Generate%20%2F%20report/',
{ page_id: 'page-1', table_name: undefined, row_id: undefined },
);
expect(get).toHaveBeenCalledWith(
'https://example.com/api/v2.1/universal-apps/app-uuid/run-script/Generate%20%2F%20report/result/42/',
{ params: { page_id: 'page-1' } },
);
});
});

Loading
Loading