How to check for two conditions using dataweave - mule

<root>
<risk>
<Category>02</Category>
<NCD>
<CoverCode>B05</CoverCode>
<CI>
<CD>3</CD>
</CI>
</NCD>
</risk>
</root>
I need to check 2 conditions to get the required payload from the above. I am using transformation as below to check the 2 conditions. I want to get the value of CD only when Category is 02 and CoverCode is B05.
%dw 1.0
%output application/xml skipNullOn="everywhere"
%namespace soapenv http://schemas.xmlsoap.org/soap/envelope/
---
{
soapenv#Envelope: {
soapenv#Body: {
component:{
coverageClassCode:payload.root.risk.NCD.CI.CD when payload.root.risk.Category == '02' && payload.root.risk.NCD.CoverCode == 'B05' otherwise "" ,
blockedDr:"N"
}
}
}
}
Thanks for your help.

Just change && to and -
%dw 1.0
%output application/xml skipNullOn="everywhere"
%namespace soapenv http://schemas.xmlsoap.org/soap/envelope/
---
{
soapenv#Envelope:{
soapenv#body:{
component:{
coverageClassCode:payload.root.risk.NCD.CI.CD when payload.root.risk.Category == '02' and payload.root.risk.NCD.CoverCode == 'B05' otherwise "",
//payload.root.risk.NCD.CI.CD when payload.root.risk.Category == '02' && payload.root.risk.NCD.CoverCode == 'B05' otherwise "" ,
blockedDr:"N"
}
}
}
}

Related

How to check if a field is null on payload from request body in Mulesoft?

I have 2 scenarios.
request body:
{
"id"="",
"name"="Jane"
}
Expected result:
"Success"
request body:
{
"id"="323",
"name"="Jane"
}
Expected result:
"id field should be emptied."
this following dataweave will help you achieve what you are looking for
%dw 1.0
%output application/json
---
{
output: 'Success' when
payload.id == null or payload.id == ""
otherwise 'id field should be emptied.'
}

How to assert a json property which can be either null(Porp:null) or has a sub schema(Porp:{ denyAny: '#boolean', assertions: '#[]' }) In KARATE DSL?

I have a Json payload to validate. And It has a property which can be either null or a sub json object. But this property exists in the json.
I tried following methods:
01
And def dnyAssertionSchema = { denyAny: '#boolean', assertions: '##[]' }
And match each policyGResponse ==
"""
{
denyAssertions: '##(dnyAssertionSchema)'
}
"""
AND
And match each policyGResponse ==
"""
{
denyAssertions: '##null dnyAssertionSchema'
}
"""
AND
This does not work as the property is not an array so I tried above second method even I couldn't find an example as such.
And match each policyGResponse ==
"""
{
denyAssertions: '##[] dnyAssertionSchema'
}
"""
The Actual response can be either
{
denyAssertions=null
}
OR
{
denyAssertions={ denyAny: true, assertions: ["a","b"] }
}
I use Karate 0.9.1
Error message I get is 'reason: actual value has 1 more key(s) than expected: {denyAssertions=null}' in first try
In second try I get 'assertion failed: path: $[3].denyAssertions, actual: {denyAny=false, assertions=[]}, expected: '##null dnyAssertionSchema', reason: not equal'
Your JSON is still not valid but anyway. Here you go:
* def schema = { denyAny: '#boolean', assertions: '#[]' }
* def response1 = { denyAssertions: { denyAny: true, assertions: ["a","b"] } }
* match response1 == { denyAssertions: '##(schema)' }
* def response2 = { denyAssertions: null }
* match response1 == { denyAssertions: '##(schema)' }

I can't send email with pdf using Amazon SDK JS, but send txt works fine?

I'm trying to build a function to send email with pdf, I need to read the file from other server and then attach it to the email.
I have test it with txt and it works fine, but when I use pdf, it attach a file that cannot be open.
That's my code until now:
let dados = {
"para": "::EMAIL::",
"body": "Olá",
"assunto": "Teste",
"from": "::EMAIL::",
"anexo": "teste.pdf" // Name of the file I want to read from server
};
request.get("::URL_SERVER::" + dados.anexo, function (error, response, body) {
let anexo;
if (!error && response.statusCode == 200) {
anexo = body;
}
let ses_mail =
`From: 'AWS SES Attchament Configuration' <${dados.from}>
To: <${dados.para}>
Subject: ${dados.assunto}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart"
--NextPart
Content-Type: text/html
${dados.body}
--NextPart
Content-Type: application/pdf; name="${dados.anexo}"
Content-Transfer-Encoding: base64
Content-Disposition:attachment
${anexo.toString("base64").replace(/([^\0]{76})/g, "$1\n")}
--NextPart`;
let params = {
RawMessage: {Data: ses_mail},
Source: `'AWS SES Attchament Configuration' <${dados.from}>`
};
let sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendRawEmail(params).promise();
return sendPromise.then(
data => {
console.log(data);
return data;
}).catch(
err => {
console.error(err.message);
throw err;
});
});
It is possible to do it with axios? I only found how to download file on my research
I could do it, but I needed to change the lib I was using to sync-request.
My final code:
let anexo = null;
try {
anexo = request( "GET", "::URL::" + dados.anexo );
} catch (err) {
console.error(err, err.stack);
return criarResposta( 404, 'Anexo não encontrado' );
}
anexo = anexo.getBody();
//return criarResposta( 200, anexo.toString("base64") );
let ses_mail =
`From: 'AWS SES Attchament Configuration' <${dados.from}>
To: <${dados.para}>
Subject: ${dados.assunto}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart"
--NextPart
Content-Type: text/html
${dados.body}
--NextPart
Content-Type: application/octet; name="arquivo.pdf"
Content-Transfer-Encoding: base64
Content-Disposition:attachment
${anexo.toString("base64")}
--NextPart`;
let params = {
RawMessage: {Data: ses_mail},
Source: `'AWS SES Attchament Configuration' <${dados.from}>`
};
sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendRawEmail(params).promise();
try {
const data = await sendPromise;
console.log(data.MessageId);
return criarResposta( 200, 'OK' );
} catch (err) {
console.error(err, err.stack);
return criarResposta( 500, 'Erro interno' );
}

Error when adding conditional check in Dataweave

I am using Anypoint Studio 6.1 and Mule 3.8.1.
I have a Boolean field coming in from a CSV file that I want to add code to clean the data e.g. turn "Y", "Yes" into true or otherwise set it to false.
I only want the code to work if the field has a value. If it is '' then I want to ignore it if it is populated then clean the data. I thought adding a when statement would be the answer but it errors in preview. How do I make this a conditional check?
Dataweave code:
(payload filter $$ > 2) map ((payload01 , indexOfPayload01) -> {
({
isPaid: true
}
when payload01.balanced != ''
and ((lower payload01.balanced == 'yes'
or (lower payload01.balanced) == 'y'
or (lower payload01.balanced ) == 'true')
otherwise {
isPaid: false
})
})
Please use below code snipped of Data Weave:
(payload filter $$ > 2) map ((payload01 , indexOfPayload01) -> {
({
isPaid: true when ((payload01.balanced != '')
and ((lower payload01.balanced) == 'yes')
or ((lower payload01.balanced) == 'y')
or ((lower payload01.balanced ) == 'true')) otherwise false
})
(payload filter $$ > 2) map ((payload01 , indexOfPayload01) -> {
({
isPaid: true
}
when payload01.balanced?
and ((lower payload01.balanced == 'yes'
or (lower payload01.balanced) == 'y'
or (lower payload01.balanced ) == 'true')
otherwise {
isPaid: false
})
})
Try this above.

Mule Datamapper JSON to XML Transformation

JSON Input Payload information, Where i have issue with.
{
"name": "xyz",
"city": "california",
"serialNumber": [
"T2323" ,
"T2332"
]
}
Expected transformed Output Payload :
<COMPANY_ITEM_INFO>
<COMPANY_NAME>xyz</COMPANY_NAME>
<COMPANY_CITY>california</COMPANY_NAME>
<Inv_Update SEGMENT="1">
<SERIALNO>T2323</SERIALNO>
</Inv_Update SEGMENT="1">
<Inv_Update SEGMENT="1">
<SERIALNO>T2332</SERIALNO>
</Inv_Update SEGMENT="1">
</COMPANY_ITEM_INFO>
Actual Output received from Datamapper only 1 element as output.
<COMPANY_ITEM_INFO>
<COMPANY_NAME>xyz</COMPANY_NAME>
<COMPANY_CITY>california</COMPANY_NAME>
<Inv_Update SEGMENT="1">
<SERIALNO>T2323</SERIALNO>
</Inv_Update SEGMENT="1">
</COMPANY_ITEM_INFO>
Datamapper Code:
Foreach 'Object' - > 'COMPANY_ITEM_INFO'
//MEL
//START -> DO NOT REMOVE
output.__id = input.__id;
//END -> DO NOT REMOVE
output.COMPANY_NAME= input.name;
output.COMPANY_CITY= input.city;
ForEach 'serialNumber' -> 'Inv_Update'
//MEL
//START -> DO NOT REMOVE
output.__id = input.__id;
output.__parent_id = input.__id;
//END -> DO NOT REMOVE
output.SERIALNO = input.array;
I know you're looking for a DataMapper solution but in case you're open to alternative, below is a Groovy implementation (since DataMapper is going away, now that Weave is coming, you should be open for options right?).
<json:json-to-object-transformer
returnClass="java.lang.Object" />
<scripting:transformer>
<scripting:script engine="groovy"><![CDATA[
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.COMPANY_ITEM_INFO {
COMPANY_NAME(payload.name)
COMPANY_CITY(payload.city)
payload.serialNumber.each { sn ->
Inv_Update(SEGMENT: '1') {
SERIALNO(sn)
}
}
}
result = writer.toString()
]]></scripting:script>
</scripting:transformer>