feat(weeztix): auto company from OAuth, remove company UI

Store company_guid after OAuth via profile API; drop company select and
companies endpoint. Coupons AJAX uses stored company only. Form request
no longer accepts company fields from the browser.

Made-with: Cursor
This commit is contained in:
2026-04-05 10:56:29 +02:00
parent 977e09d8ac
commit 6561bda30d
7 changed files with 60 additions and 156 deletions

View File

@@ -769,16 +769,12 @@ document.addEventListener('alpine:init', () => {
Alpine.data('weeztixSetup', (cfg) => ({
pageId: cfg.pageId,
companiesUrl: cfg.companiesUrl,
couponsUrl: cfg.couponsUrl,
csrf: cfg.csrf,
isConnected: cfg.isConnected === true,
callbackUrl: cfg.callbackUrl,
errorMessage: '',
companies: [],
coupons: [],
companyGuid: '',
companyName: '',
couponGuid: '',
couponName: '',
codePrefix: 'PREREG',
@@ -797,19 +793,12 @@ document.addEventListener('alpine:init', () => {
} else {
this.usageCount = 1;
}
this.companyGuid = cfg.existing.company_guid || '';
this.companyName = cfg.existing.company_name || '';
this.couponGuid = cfg.existing.coupon_guid || '';
this.couponName = cfg.existing.coupon_name || '';
}
if (this.isConnected) {
await this.loadCompanies();
if (this.companyGuid) {
await this.loadCouponsForGuid(this.companyGuid);
}
} else if (cfg.existing && (cfg.existing.company_guid || cfg.existing.coupon_guid)) {
// Show saved choices even when not connected (e.g. expired refresh); lists are from DB only.
this.ensureSelectedCompanyInList();
await this.loadCoupons();
} else if (cfg.existing && cfg.existing.coupon_guid) {
this.ensureSelectedCouponInList();
}
},
@@ -829,18 +818,6 @@ document.addEventListener('alpine:init', () => {
return { res, data };
},
syncCompanyNameFromSelection() {
if (!this.companyGuid) {
this.companyName = '';
return;
}
const c = this.companies.find((x) => x.guid === this.companyGuid);
if (c && typeof c.name === 'string' && c.name.trim() !== '') {
this.companyName = c.name.trim();
}
// If API row has no name or list is still loading, keep companyName from server (DB).
},
syncCouponName() {
if (!this.couponGuid) {
this.couponName = '';
@@ -852,18 +829,6 @@ document.addEventListener('alpine:init', () => {
}
},
ensureSelectedCompanyInList() {
const guid = this.companyGuid;
if (!guid || this.companies.some((x) => x.guid === guid)) {
return;
}
const label =
typeof this.companyName === 'string' && this.companyName.trim() !== ''
? this.companyName.trim()
: guid;
this.companies = [{ guid, name: label }, ...this.companies];
},
ensureSelectedCouponInList() {
const guid = this.couponGuid;
if (!guid || this.coupons.some((x) => x.guid === guid)) {
@@ -876,36 +841,9 @@ document.addEventListener('alpine:init', () => {
this.coupons = [{ guid, name: label }, ...this.coupons];
},
async loadCompanies() {
async loadCoupons() {
this.errorMessage = '';
const { res, data } = await this.postJson(this.companiesUrl, { page_id: this.pageId });
if (!res.ok) {
this.errorMessage = data.message || this.strings.genericError;
this.ensureSelectedCompanyInList();
return;
}
this.companies = Array.isArray(data.companies) ? data.companies : [];
this.ensureSelectedCompanyInList();
this.syncCompanyNameFromSelection();
},
async onCompanyChange() {
this.syncCompanyNameFromSelection();
this.couponGuid = '';
this.couponName = '';
this.coupons = [];
if (!this.companyGuid) {
return;
}
await this.loadCouponsForGuid(this.companyGuid);
},
async loadCouponsForGuid(guid) {
this.errorMessage = '';
const { res, data } = await this.postJson(this.couponsUrl, {
page_id: this.pageId,
company_guid: guid,
});
const { res, data } = await this.postJson(this.couponsUrl, { page_id: this.pageId });
if (!res.ok) {
this.errorMessage = data.message || this.strings.loadCouponsError;
this.ensureSelectedCouponInList();
@@ -915,37 +853,6 @@ document.addEventListener('alpine:init', () => {
this.ensureSelectedCouponInList();
this.syncCouponName();
},
/** Prefer human-readable label; skip API "names" that are just the GUID / UUID. */
companyLabel(c) {
if (!c || typeof c.guid !== 'string') {
return '';
}
const g = c.guid;
const isBadLabel = (s) => {
const t = typeof s === 'string' ? s.trim() : '';
return (
t === '' ||
t.toLowerCase() === g.toLowerCase() ||
this.stringLooksLikeUuid(t)
);
};
const fromApi = typeof c.name === 'string' ? c.name : '';
if (!isBadLabel(fromApi)) {
return fromApi.trim();
}
if (this.companyGuid === g && !isBadLabel(this.companyName)) {
return String(this.companyName).trim();
}
return g;
},
stringLooksLikeUuid(s) {
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/.test(
String(s),
);
},
}));
});