Skip to content

Commit 5cc6f68

Browse files
authored
Merge pull request #74 from oracle-samples/rest-file-api
rest-file-api
2 parents cfe8592 + 53d42d8 commit 5cc6f68

19 files changed

+13173
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# dependencies
2+
**/node_modules/**
3+
4+
# ides
5+
**/.idea/**
6+
7+
# test coverage folder
8+
**/coverage/**
9+
10+
# suitecloud
11+
project.json
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Copyright (c) 2021 Oracle and/or its affiliates.
2+
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
Subject to the condition set forth below, permission is hereby granted to any
6+
person obtaining a copy of this software, associated documentation and/or data
7+
(collectively the "Software"), free of charge and under any and all copyright
8+
rights in the Software, and any and all patent rights owned or freely
9+
licensable by each licensor hereunder covering either (i) the unmodified
10+
Software as contributed to or provided by such licensor, or (ii) the Larger
11+
Works (as defined below), to deal in both
12+
13+
(a) the Software, and
14+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
one is included with the Software (each a “Larger Work” to which the Software
16+
is contributed by such licensors),
17+
18+
without restriction, including without limitation the rights to copy, create
19+
derivative works of, display, perform, and distribute the Software and make,
20+
use, sell, offer for sale, import, export, have made, and have sold the
21+
Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
either these or other terms.
23+
24+
This license is subject to the following condition:
25+
The above copyright notice and either this complete permission notice or at
26+
a minimum a reference to the UPL must be included in all copies or
27+
substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p align="center"><a href="#"><img width="250" src="oracle_netsuite_logo.png"></a></p>
2+
3+
# Summary
4+
This SuiteScript project uses a Restlet script to create a new file to be attached to vendor records after a purchase order is saved. Data to post to the newly created file will be defined in a client script.
5+
6+
# Key Concepts
7+
This sample demonstrates the following concepts:
8+
9+
* **SuiteScript 2.1** – This SuiteScript version is the latest available version and includes new language features that are not part of SuiteScript 2.0. This sample uses some SuiteScript 2.1 features, such as arrow functions. For more information, see [SuiteScript 2.1](https://system.netsuite.com/app/help/helpcenter.nl?fid=chapter_156042690639.html).
10+
11+
* **Client script type** – Client scripts are scripts that are executed by predefined event triggers in the client browser. They can validate user-entered data and auto-populate fields or sublists at various form events. For more information, see [SuiteScript 2.x Client Script Type](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4387798404.html).
12+
13+
* **Restlet script type** - A RESTlet is a SuiteScript that you make available for other applications to call. It can be called from either an external application or from another script within NetSuite. A RESTlet executes only when it is called and in some cases, returns a value to the calling application.
14+
15+
RESTlets can be useful when you want to bring data into NetSuite from another system, or if you want to extract data from NetSuite. RESTlets can also be used in combination with other scripts to customize the behavior of a page within NetSuite. For more information, see [SuiteScript 2.x RESTlet Script Type](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4387799403.html).
16+
17+
# SuiteScript Version
18+
This sample uses SuiteScript 2.1. For more information about this SuiteScript version, see [SuiteScript 2.1](https://system.netsuite.com/app/help/helpcenter.nl?fid=chapter_156042690639.html).
19+
20+
# Loaded Modules
21+
This sample loads and uses the following SuiteScript modules:
22+
23+
* **N/https** – To communicate between SuiteScript scripts and RESTlets. For more information, see [N/https](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4418229131.html).
24+
* **N/url** - To determine URL navigation paths within NetSuite and format URL strings. For more information, see [N/url](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4358552918.html).
25+
* **N/file** - To work with files within NetSuite. You can use this module to upload files to the NetSuite File Cabinet, as well as send files as attachments without uploading them to the File Cabinet. For more information, see [N/file Module](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4205693274.html).
26+
* **N/record** - To work with NetSuite records. You can use this module to create, delete, copy, load, or make changes to a record. For more information, see [N/record Module](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4267255811.html).
27+
* **N/log** – To log errors that occur during execution. For more information, see [N/log Module](https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4574548135.html).
28+
29+
# Script Type and Entry Points
30+
This sample uses a user event script type with the following entry points:
31+
32+
* `post` – Defines the function that is executed when a post request is sent to a RESTlet. Typically used for posting, such as creating a NetSuite record.
33+
* `saveRecord` -
34+
Defines the function that is executed when a record is saved (after the submit button is pressed but before the form is submitted).
35+
36+
# Test the Solution
37+
1. Go to Transactions > Purchases > Enter Purchase Orders.
38+
2. Fill out the required fields for the purchase order.
39+
3. Click Save.
40+
41+
When the record is saved, a new file will be created and attached to the vendor record of the purchase order. To view the attached file on the vendor record:
42+
43+
1. Go to Lists > Relationships > Vendors.
44+
2. Click View on the vendor record used on the purchase order.
45+
3. Click the Communication tab.
46+
4. Under the Files subtab, confirm the file was attached to the record.
47+
48+
<p align="center"><a href="#"><img width="250" src="vendor-record.png"></a></p>
49+
50+
## [License](./LICENSE.txt)
51+
Copyright (c) 2021 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import script from '../src/FileCabinet/SuiteScripts/cs_call_restlet'
2+
3+
import url from 'N/url'
4+
import https from 'N/https'
5+
import CurrentRecord from 'N/currentRecord/instance'
6+
7+
jest.mock('N/url')
8+
jest.mock('N/currentRecord/instance')
9+
jest.mock('N/record')
10+
jest.mock('N/https')
11+
12+
beforeEach(() => {
13+
jest.clearAllMocks()
14+
})
15+
16+
CurrentRecord.Type = {
17+
VENDOR: 'VENDOR'
18+
}
19+
20+
describe('client script test', () => {
21+
it('should test saveRecord function', async () => {
22+
// given
23+
const context = {
24+
currentRecord: CurrentRecord
25+
}
26+
url.resolveScript.mockReturnValue(
27+
'/app/site/hosting/restlet.nl?script=158&deploy=1')
28+
29+
// when
30+
await script.saveRecord(context)
31+
32+
// then
33+
expect(CurrentRecord.getValue).toHaveBeenCalled()
34+
expect(https.post.promise).toHaveBeenCalled()
35+
})
36+
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import script from '../src/FileCabinet/SuiteScripts/rs_file_api'
2+
3+
import file from 'N/file'
4+
import File from 'N/file/instance'
5+
6+
import record from 'N/record'
7+
8+
import log from 'N/log'
9+
10+
jest.mock('N/file')
11+
jest.mock('N/file/instance')
12+
jest.mock('N/record')
13+
jest.mock('N/log')
14+
15+
beforeEach(() => {
16+
jest.clearAllMocks()
17+
})
18+
19+
describe('restlet script test', () => {
20+
it('should test for errors in post', () => {
21+
// given
22+
const requestBody = {
23+
files: [File]
24+
}
25+
26+
File.name = 'filename1'
27+
28+
// when
29+
script.post(requestBody)
30+
31+
// then
32+
expect(log.error).toHaveBeenCalled()
33+
})
34+
it('should test post function', () => {
35+
// given
36+
const requestBody = {
37+
files: [File],
38+
recordType: 'VENDOR',
39+
internalId: 889
40+
}
41+
42+
File.name = 'filename1'
43+
File.folder = 'foldername1'
44+
File.fileType = 'PLAINTEXT'
45+
File.contents = 'hello world'
46+
47+
file.create.mockReturnValue(File)
48+
File.save.mockReturnValue(123)
49+
50+
// when
51+
script.post(requestBody)
52+
53+
// then
54+
expect(record.attach).toHaveBeenCalledWith({
55+
record: {
56+
type: 'file',
57+
id: 123
58+
},
59+
to: {
60+
type: 'VENDOR',
61+
id: 889
62+
}
63+
})
64+
})
65+
})
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { defineConfig } from 'eslint/config'
2+
import globals from 'globals'
3+
import jsdoc from 'eslint-plugin-jsdoc'
4+
import neostandard from 'neostandard'
5+
6+
export default defineConfig([
7+
...neostandard(),
8+
{
9+
files: ['**/*.js'],
10+
plugins: { jsdoc },
11+
languageOptions: {
12+
globals: {
13+
...globals.browser,
14+
...globals.jest,
15+
...globals.amd,
16+
Atomics: 'readonly',
17+
SharedArrayBuffer: 'readonly',
18+
},
19+
ecmaVersion: 2018,
20+
sourceType: 'script',
21+
},
22+
23+
rules: {
24+
'prefer-const': 'error',
25+
'jsdoc/require-jsdoc': 'warn',
26+
'no-var': 'error',
27+
'no-duplicate-imports': 'error',
28+
'no-label-var': 'error',
29+
'no-unsafe-finally': 'error',
30+
'@stylistic/indent': ['warn', 2],
31+
'no-unused-vars': 'error',
32+
'@stylistic/quotes': ['warn', 'single'],
33+
'no-undef-init': 'error',
34+
'@stylistic/no-trailing-spaces': 'off',
35+
'@stylistic/max-len': [
36+
'error', {
37+
code: 80,
38+
tabWidth: 4,
39+
}],
40+
'prefer-arrow-callback': 'error',
41+
'max-lines': ['error', 500],
42+
'@stylistic/function-call-argument-newline': ['error', 'never'],
43+
'@stylistic/array-element-newline': ['error', 'never'],
44+
'vars-on-top': 'error',
45+
},
46+
}])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const SuiteCloudJestConfiguration = require('@oracle/suitecloud-unit-testing/jest-configuration/SuiteCloudJestConfiguration')
2+
const cliConfig = require('./suitecloud.config')
3+
4+
module.exports = SuiteCloudJestConfiguration.build({
5+
projectFolder: cliConfig.defaultProjectFolder,
6+
projectType: SuiteCloudJestConfiguration.ProjectType.ACP,
7+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"typeAcquisition": {
3+
"include": ["jest"]
4+
}
5+
}
3.34 KB
Loading

0 commit comments

Comments
 (0)