Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
bxxf committed Apr 3, 2023
1 parent 38f8da6 commit da7b923
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
4 changes: 1 addition & 3 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ const someFunction = async () => {
const result = await sfmc
.dataExtension("DataExtensionName")
.get()
.where("EmailAddress", "eq", "[email protected]")
console.log(result);


const resultSoap = await sfmc
.dataExtension("DataExtensionName")
.soap
.get(["Name", "Name2"], {
queryAllAccounts: true,
})
.where("Name", "equals", "test");

console.log(resultSoap);
};

Expand Down
16 changes: 7 additions & 9 deletions src/utils/soap/soap-client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { SfmcHelper } from "../../sfmc-ts";
import fetch from "node-fetch";
import { parseStringPromise } from "xml2js";

import {
createDataExtensionBody,
createFieldsXml,
} from "../../models/soap/create";

import { deleteDataExtensionBody } from "../../models/soap/delete";
import { retrieveDataExtensionBody } from "../../models/soap/retrieve";
import { parseStringPromise } from "xml2js";
import { SfmcSoapFilter, createFilterXml } from "./soap-filter";
import { SfmcHelper } from "../../sfmc-ts";


enum SoapAction {
Create = "Create",
Expand Down Expand Up @@ -80,7 +83,6 @@ export class SfmcDataExtensionSoap implements ISoapRequest {
): Promise<any> {
await this.helper.authenticate();
const soapEnvelope = this.buildSoapEnvelope(requestBody);
console.log(soapEnvelope);
let response: any;
try {
response = await fetch(
Expand Down Expand Up @@ -151,8 +153,7 @@ export class SfmcDataExtensionSoap implements ISoapRequest {
.join("");

const filterXml = filters.length > 0 ? createFilterXml(filters) : "";



const response = await this.sendSoapRequest(
retrieveDataExtensionBody(
this.objectKey,
Expand All @@ -165,12 +166,9 @@ export class SfmcDataExtensionSoap implements ISoapRequest {

const envelopeBody = response["soap:Envelope"]["soap:Body"];
const retrieveResponse = envelopeBody.RetrieveResponseMsg;




const overallStatus = retrieveResponse.OverallStatus;
const results = retrieveResponse.Results;


const resultsArray = Array.isArray(results)
? results
Expand Down
1 change: 1 addition & 0 deletions tests/mock-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const apiConfig = {
clientSecret: "test_client_secret",
authEndpoint: "https://auth.example.com",
restEndpoint: "https://rest.example.com",
soapEndpoint: "https://soap.example.com",
businessUnitId: "test_business_unit_id",
};
export const authResponse = {
Expand Down
50 changes: 50 additions & 0 deletions tests/sfmc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ComparisonEnum } from "../src/index";
import { SfmcHelper } from "../src/sfmc-ts";
import { SfmcQueryBuilder } from "../src/utils/query-builder";
import { apiConfig, authResponse, authRequestBody } from "./mock-data";
import { SfmcDataExtensionSoap } from "../src/utils/soap/soap-client";

describe("SfmcHelper", () => {
nock.disableNetConnect();
Expand Down Expand Up @@ -178,4 +179,53 @@ describe("SfmcHelper", () => {

expect(result).toEqual(rowsetResponse.items.map((item) => item.values));
});

it("should fetch via soap api", async () => {
const dataExtension = "test_data_extension_key";
const fields = ["test_column_1", "test_column_2"];
const testValues = {
test_column_1: "value_1",
test_column_2: "value_2",
};

const soapResponse = `
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<RetrieveResponseMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">
<OverallStatus>OK</OverallStatus>
<RequestID>123456789</RequestID>
<Results xsi:type="DataExtensionObject">
<PartnerKey xsi:nil="true"/>
<ObjectID xsi:nil="true"/>
<Type>DataExtensionObject</Type>
<Properties>
${fields
.map(
(field) =>
// @ts-ignore
`<Property><Name>${field}</Name><Value>${testValues[field]}</Value></Property>`
)
.join("")}
</Properties>
</Results>
<RequestType>Retrieve</RequestType>
</RetrieveResponseMsg>
</soap:Body>
</soap:Envelope>
`;

nock(apiConfig.soapEndpoint)
.persist()
.post("/Service.asmx")
.reply(200, soapResponse);

const de = new SfmcDataExtensionSoap(helper, dataExtension);
const result = await de.get({
fields,
filters: [],
});

expect(result).toEqual([testValues]);
});
});

0 comments on commit da7b923

Please sign in to comment.