77 lines
3.3 KiB
JavaScript
77 lines
3.3 KiB
JavaScript
|
|
import crypto from 'crypto';
|
||
|
|
import https from 'https';
|
||
|
|
import fs from 'fs';
|
||
|
|
|
||
|
|
const header = Buffer.from(JSON.stringify({ alg: 'RS256' })).toString('base64url');
|
||
|
|
const now = Math.floor(Date.now() / 1000);
|
||
|
|
const claims = {
|
||
|
|
iss: '3MVG9RGDcVlytYtcgAeR2ANSn3O2OMs6MBQgA.tpHlObl8_eArqCzm0u0E73PfcBZxkCvs5DAiDb8yFYtcL5Q',
|
||
|
|
sub: 'portal.integration@asolutions.co.jp.playground',
|
||
|
|
aud: 'https://asolutions--playground.sandbox.my.salesforce.com',
|
||
|
|
exp: now + 300
|
||
|
|
};
|
||
|
|
const payload = Buffer.from(JSON.stringify(claims)).toString('base64url');
|
||
|
|
const unsigned = header + '.' + payload;
|
||
|
|
const keyPem = fs.readFileSync('apps/bff/secrets/sf-sandbox-private.pem', 'utf8');
|
||
|
|
const sign = crypto.createSign('RSA-SHA256');
|
||
|
|
sign.update(unsigned);
|
||
|
|
const sig = sign.sign(keyPem, 'base64url');
|
||
|
|
const jwt = unsigned + '.' + sig;
|
||
|
|
|
||
|
|
function sfRequest(accessToken, instanceUrl, soql) {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const url = new URL(instanceUrl + '/services/data/v59.0/query?q=' + encodeURIComponent(soql));
|
||
|
|
https.get({
|
||
|
|
hostname: url.hostname,
|
||
|
|
path: url.pathname + url.search,
|
||
|
|
headers: { 'Authorization': 'Bearer ' + accessToken }
|
||
|
|
}, (res) => {
|
||
|
|
let body = '';
|
||
|
|
res.on('data', d => body += d);
|
||
|
|
res.on('end', () => resolve(JSON.parse(body)));
|
||
|
|
}).on('error', reject);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function getToken() {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const postData = 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=' + jwt;
|
||
|
|
const url = new URL('https://asolutions--playground.sandbox.my.salesforce.com/services/oauth2/token');
|
||
|
|
const req = https.request({
|
||
|
|
hostname: url.hostname, path: url.pathname, method: 'POST',
|
||
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData) }
|
||
|
|
}, (res) => {
|
||
|
|
let body = '';
|
||
|
|
res.on('data', d => body += d);
|
||
|
|
res.on('end', () => resolve(JSON.parse(body)));
|
||
|
|
});
|
||
|
|
req.on('error', reject);
|
||
|
|
req.write(postData);
|
||
|
|
req.end();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const token = await getToken();
|
||
|
|
if (!token.access_token) {
|
||
|
|
console.error('Auth failed:', token);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('=== SIM Products (category = Sim) ===');
|
||
|
|
const simProducts = await sfRequest(token.access_token, token.instance_url,
|
||
|
|
"SELECT Id, Name, StockKeepingUnit, Product2Categories1__c, Item_Class__c, Portal_Accessible__c, Portal_Catalog__c, SIM_Plan_Type__c, IsActive FROM Product2 WHERE Product2Categories1__c = 'Sim' LIMIT 30"
|
||
|
|
);
|
||
|
|
console.log('Total:', simProducts.totalSize);
|
||
|
|
for (const r of (simProducts.records || [])) {
|
||
|
|
console.log(` ${r.Name} | SKU: ${r.StockKeepingUnit} | Class: ${r.Item_Class__c} | Accessible: ${r.Portal_Accessible__c} | Catalog: ${r.Portal_Catalog__c} | PlanType: ${r.SIM_Plan_Type__c} | Active: ${r.IsActive}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('\n=== Pricebook Entries for Portal Pricebook (SIM products) ===');
|
||
|
|
const pbeQuery = await sfRequest(token.access_token, token.instance_url,
|
||
|
|
"SELECT Id, Product2.Name, Product2.StockKeepingUnit, UnitPrice, IsActive FROM PricebookEntry WHERE Pricebook2Id = '01sTL000008eLVlYAM' AND Product2.Product2Categories1__c = 'Sim' AND IsActive = true LIMIT 30"
|
||
|
|
);
|
||
|
|
console.log('Total:', pbeQuery.totalSize);
|
||
|
|
for (const r of (pbeQuery.records || [])) {
|
||
|
|
console.log(` ${r.Product2.Name} | SKU: ${r.Product2.StockKeepingUnit} | Price: ${r.UnitPrice} | Active: ${r.IsActive}`);
|
||
|
|
}
|