Skip to content

Commit

Permalink
feat(foxy-transaction): add xml datafeed controls
Browse files Browse the repository at this point in the history
  • Loading branch information
pheekus committed Aug 22, 2024
1 parent c555fcd commit 1dbf5fb
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 1 deletion.
91 changes: 91 additions & 0 deletions src/elements/public/Transaction/Transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,22 @@ describe('Transaction', () => {
expect(customElements.get('foxy-internal-async-list-control')).to.exist;
});

it('imports and defines foxy-internal-summary-control', () => {
expect(customElements.get('foxy-internal-summary-control')).to.exist;
});

it('imports and defines foxy-internal-switch-control', () => {
expect(customElements.get('foxy-internal-switch-control')).to.exist;
});

it('imports and defines foxy-internal-form', () => {
expect(customElements.get('foxy-internal-form')).to.exist;
});

it('imports and defines foxy-internal-transaction-post-action-control', () => {
expect(customElements.get('foxy-internal-transaction-post-action-control')).to.exist;
});

it('imports and defines foxy-internal-transaction-customer-control', () => {
expect(customElements.get('foxy-internal-transaction-customer-control')).to.exist;
});
Expand Down Expand Up @@ -139,6 +151,46 @@ describe('Transaction', () => {
});
});

it('always keeps datafeed controls readonly', () => {
const element = new Transaction();
expect(element.readonlySelector.matches('datafeed', true)).to.be.true;
});

it("hides XML datafeed controls when store doesn't have XML datafeed enabled", async () => {
const router = createRouter();
const element = await fixture<Transaction>(html`
<foxy-transaction
href="https://demo.api/hapi/transactions/0"
@fetch=${(evt: FetchEvent) => !evt.defaultPrevented && router.handleEvent(evt)}
>
</foxy-transaction>
`);

await waitUntil(
() => {
if (!element.in({ idle: 'snapshot' })) return false;
const nucleons = element.renderRoot.querySelectorAll<NucleonElement<any>>('foxy-nucleon');
return [...nucleons].every(nucleon => nucleon.in({ idle: 'snapshot' }));
},
'',
{ timeout: 5000 }
);

expect(element.hiddenSelector.matches('datafeed', true)).to.be.true;
expect(element.hiddenSelector.matches('actions:resend-datafeed', true)).to.be.true;

const store = await getTestData<Resource<Rels.Store>>('https://demo.api/hapi/stores/0');
store.use_webhook = true;
Transaction.Rumour('').share({
source: 'https://demo.api/hapi/stores/0',
data: store,
});

await element.requestUpdate();
expect(element.hiddenSelector.matches('datafeed', true)).to.be.false;
expect(element.hiddenSelector.matches('actions:resend-datafeed', true)).to.be.false;
});

it('renders a form header', () => {
const form = new Transaction();
const renderHeaderMethod = stub(form, 'renderHeader');
Expand Down Expand Up @@ -560,6 +612,24 @@ describe('Transaction', () => {
expect(control).to.have.property('localName', 'foxy-internal-transaction-actions-control');
});

it('renders a post action control for refeeding XML datafeed', async () => {
const router = createRouter();
const element = await fixture<Transaction>(html`
<foxy-transaction
href="https://demo.api/hapi/transactions/1"
@fetch=${(evt: FetchEvent) => !evt.defaultPrevented && router.handleEvent(evt)}
>
</foxy-transaction>
`);

await waitUntil(() => element.in({ idle: 'snapshot' }));
const control = element.renderRoot.querySelector('[infer="actions resend-datafeed"]');

expect(control).to.exist;
expect(control).to.have.property('localName', 'foxy-internal-transaction-post-action-control');
expect(control).to.have.attribute('href', element.data!._links['fx:process_webhook'].href);
});

it('renders customer info as control', async () => {
const router = createRouter();
const element = await fixture<Transaction>(html`
Expand Down Expand Up @@ -831,4 +901,25 @@ describe('Transaction', () => {
expect(refeedRequest?.url).to.equal('https://demo.api/virtual/empty?status=200');
expect(await refeedRequest?.json()).to.deep.equal({ refeed_hooks: [0], event: 'refeed' });
});

it('renders XML datafeed status as control', async () => {
const router = createRouter();
const element = await fixture<Transaction>(html`
<foxy-transaction
href="https://demo.api/hapi/transactions/1"
@fetch=${(evt: FetchEvent) => !evt.defaultPrevented && router.handleEvent(evt)}
>
</foxy-transaction>
`);

await waitUntil(() => element.in({ idle: 'snapshot' }));

const summary = element.renderRoot.querySelector('[infer="datafeed"]');
expect(summary).to.exist;
expect(summary).to.have.property('localName', 'foxy-internal-summary-control');

const status = summary?.querySelector('[infer="data-is-fed"');
expect(status).to.exist;
expect(status).to.have.property('localName', 'foxy-internal-switch-control');
});
});
22 changes: 21 additions & 1 deletion src/elements/public/Transaction/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ export class Transaction extends Base<Data> {
];

get readonlySelector(): BooleanSelector {
const alwaysMatch = ['billing-addresses', 'webhooks:dialog:url', super.readonlySelector];
const alwaysMatch = [
'billing-addresses',
'datafeed',
'webhooks:dialog:url',
super.readonlySelector,
];

const isEditable = Boolean(this.data?._links['fx:void'] ?? this.data?._links['fx:refund']);
if (!isEditable) alwaysMatch.push('items', 'attributes', 'custom-fields');
return new BooleanSelector(alwaysMatch.join(' ').trim());
Expand Down Expand Up @@ -109,6 +115,10 @@ export class Transaction extends Base<Data> {
alwaysMatch.unshift('not=customer,subscription,custom-fields,attributes');
}

if (!this.__storeLoader?.data?.use_webhook) {
alwaysMatch.unshift('datafeed', 'actions:resend-datafeed');
}

return new BooleanSelector(alwaysMatch.join(' ').trim());
}

Expand Down Expand Up @@ -187,6 +197,12 @@ export class Transaction extends Base<Data> {
return html`
<foxy-internal-transaction-actions-control infer="actions">
</foxy-internal-transaction-actions-control>
<foxy-internal-transaction-post-action-control
infer="actions resend-datafeed"
href=${ifDefined(this.data?._links['fx:process_webhook'].href)}
>
</foxy-internal-transaction-post-action-control>
`;
}

Expand Down Expand Up @@ -324,6 +340,10 @@ export class Transaction extends Base<Data> {
>
</foxy-internal-async-list-control>
<foxy-internal-summary-control infer="datafeed">
<foxy-internal-switch-control infer="data-is-fed"></foxy-internal-switch-control>
</foxy-internal-summary-control>
<foxy-nucleon
class="hidden"
infer=""
Expand Down
3 changes: 3 additions & 0 deletions src/elements/public/Transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import '../ItemForm/index';
import '../I18n/index';

import '../../internal/InternalAsyncListControl/index';
import '../../internal/InternalSummaryControl/index';
import '../../internal/InternalSwitchControl/index';
import '../../internal/InternalForm/index';

import './internal/InternalTransactionPostActionControl/index';
import './internal/InternalTransactionCustomerControl/index';
import './internal/InternalTransactionActionsControl/index';
import './internal/InternalTransactionSummaryControl/index';
Expand Down
108 changes: 108 additions & 0 deletions src/static/translations/admin-subscription-form/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,103 @@
}
}
},
"webhooks": {
"label": "Webhooks",
"helper_text": "This list shows v2 webhooks only. Legacy v1 webhooks are available in Settings > Integrations and on admin.foxycart.com.",
"dialog": {
"close": "Close",
"cancel": "Cancel",
"header_update": "Webhook",
"webhook-form": {
"header": {
"title_existing": "{{ name }}",
"subtitle_transaction": ""
},
"url": {
"label": "URL",
"helper_text": ""
},
"statuses": {
"label": "Runs",
"pagination": {
"first": "First",
"last": "Last",
"next": "Next",
"pagination": "{{from}}-{{to}} out of {{total}}",
"previous": "Previous",
"card": {
"status_successful": "Successful",
"status_pending": "Pending",
"status_failed": "Failed",
"date": "{{ value, date }} at {{ value, time }}",
"spinner": {
"loading_busy": "Loading",
"loading_error": "Unknown error",
"loading_empty": "No runs for this transaction yet"
}
}
}
},
"logs": {
"label": "Logs",
"pagination": {
"first": "First",
"last": "Last",
"next": "Next",
"pagination": "{{from}}-{{to}} out of {{total}}",
"previous": "Previous",
"card": {
"date": "{{ value, date }} at {{ value, time }}",
"spinner": {
"loading_busy": "Loading",
"loading_error": "Unknown error",
"loading_empty": "No runs for this transaction yet"
}
}
}
},
"spinner": {
"refresh": "Refresh",
"loading_busy": "Loading",
"loading_error": "Unknown error"
}
}
},
"pagination": {
"select_button_text": "Select",
"cancel_button_text": "Cancel",
"refeed_bulk_action_caption_idle": "Refeed ({{ count }})",
"refeed_bulk_action_caption_busy": "Refeeding...",
"refeed_bulk_action_notification_done": "Selected webhooks were sent successfully.",
"refeed_bulk_action_notification_fail": "Failed to refeed selected webhooks.",
"first": "First",
"last": "Last",
"next": "Next",
"pagination": "{{from}}-{{to}} out of {{total}}",
"previous": "Previous",
"card": {
"status_successful": "OK",
"status_pending": "Pending",
"status_failed": "Failed",
"status_none": "No runs yet",
"spinner": {
"loading_busy": "Loading",
"loading_empty": "No webhooks",
"loading_error": "Unknown error"
}
}
}
},
"datafeed": {
"label": "Legacy XML datafeed",
"helper_text": "To resend this webhook, use the Resend datafeed button at the top of the page.",
"data-is-fed": {
"label": "Status",
"helper_text": "",
"checked": "Sent",
"unchecked": "Not sent"
}
},
"summary": {
"total_shipping": "Shipping",
"total_tax": "Tax",
Expand Down Expand Up @@ -1808,6 +1905,17 @@
"cancel": "No"
}
},
"resend-datafeed": {
"idle": "Resend datafeed",
"busy": "Resending datafeed...",
"fail": "Failed to resend datafeed",
"confirm": {
"header": "Resend datafeed",
"message": "Are you sure you'd like to resend XML datafeed for this transaction?",
"confirm": "Yes",
"cancel": "No"
}
},
"subscription": {
"caption": "Go to subscription"
}
Expand Down
21 changes: 21 additions & 0 deletions src/static/translations/transaction/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,16 @@
}
}
},
"datafeed": {
"label": "Legacy XML datafeed",
"helper_text": "To resend this webhook, use the Resend datafeed button at the top of the page.",
"data-is-fed": {
"label": "Status",
"helper_text": "",
"checked": "Sent",
"unchecked": "Not sent"
}
},
"summary": {
"total_shipping": "Shipping",
"total_tax": "Tax",
Expand Down Expand Up @@ -1656,6 +1666,17 @@
"cancel": "No"
}
},
"resend-datafeed": {
"idle": "Resend datafeed",
"busy": "Resending datafeed...",
"fail": "Failed to resend datafeed",
"confirm": {
"header": "Resend datafeed",
"message": "Are you sure you'd like to resend XML datafeed for this transaction?",
"confirm": "Yes",
"cancel": "No"
}
},
"subscription": {
"caption": "Go to subscription"
}
Expand Down

0 comments on commit 1dbf5fb

Please sign in to comment.