@redonvn/zalo-ads-sdk 0.2.15 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -284,6 +284,7 @@ function parseZaloCookieInput(rawInput) {
284
284
  }
285
285
  var ZaloAuthManager = class {
286
286
  explicitAccountKey;
287
+ accountScope;
287
288
  zpsid;
288
289
  getZpsidFn;
289
290
  extraCookies;
@@ -301,6 +302,7 @@ var ZaloAuthManager = class {
301
302
  throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
302
303
  }
303
304
  this.explicitAccountKey = normalizedOptions.integrationId ?? normalizedOptions.accountKey;
305
+ this.accountScope = normalizedOptions.accountScope;
304
306
  this.extraCookies = { ...normalizedOptions.extraCookies ?? {} };
305
307
  if (normalizedOptions.zpsid) {
306
308
  const parsed = parseZaloCookieInput(normalizedOptions.zpsid);
@@ -348,6 +350,12 @@ var ZaloAuthManager = class {
348
350
  getSessionStore() {
349
351
  return this.sessionStore;
350
352
  }
353
+ getAccountScope() {
354
+ return this.accountScope;
355
+ }
356
+ setAccountScope(scope) {
357
+ this.accountScope = scope;
358
+ }
351
359
  /**
352
360
  * Cập nhật `zpsid` mới và xóa toàn bộ cache phiên cũ trên cả RAM lẫn Redis.
353
361
  */
@@ -637,11 +645,58 @@ var ZaloAuthManager = class {
637
645
  const whoamiJson = await whoamiRes.json().catch(() => null);
638
646
  if (whoamiJson?.data) {
639
647
  adsProfile = whoamiJson.data;
640
- if (adsProfile.id) {
641
- cookies["ads_user_id"] = String(adsProfile.id);
648
+ }
649
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0" && !this.accountScope.isRoot && !explicitPlaySession) {
650
+ const memberIdStr = String(this.accountScope.memberId);
651
+ const switchUrls = [];
652
+ if (this.accountScope.accountType === "ACCESSOR") {
653
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
654
+ } else if (this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || this.accountScope.accountType === "AGENCY_SUB_ACCOUNT") {
655
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
656
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
657
+ } else {
658
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
659
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
660
+ }
661
+ for (const switchUrl of switchUrls) {
662
+ try {
663
+ const switchRes = await this.fetchImpl(switchUrl, {
664
+ method: "GET",
665
+ headers: {
666
+ Cookie: this.formatCookieHeader(cookies),
667
+ "User-Agent": this.userAgent,
668
+ Accept: "application/json, text/plain, */*"
669
+ },
670
+ redirect: "manual"
671
+ });
672
+ this.mergeSetCookies(switchRes, cookies);
673
+ const location = switchRes.headers.get("location") || "";
674
+ if (location && !location.includes("/client/denied")) {
675
+ const switchedWhoamiRes = await this.fetchImpl("https://ads.zalo.me/api/whoami", {
676
+ method: "GET",
677
+ headers: {
678
+ Cookie: this.formatCookieHeader(cookies),
679
+ "User-Agent": this.userAgent,
680
+ Accept: "application/json"
681
+ }
682
+ });
683
+ this.mergeSetCookies(switchedWhoamiRes, cookies);
684
+ const switchedJson = await switchedWhoamiRes.json().catch(() => null);
685
+ if (switchedJson?.data) {
686
+ adsProfile = switchedJson.data;
687
+ }
688
+ break;
689
+ }
690
+ } catch {
691
+ }
642
692
  }
693
+ }
694
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0") {
695
+ cookies["ads_user_id"] = String(this.accountScope.memberId);
643
696
  } else if (this.extraCookies?.ads_user_id) {
644
- cookies["ads_user_id"] = this.extraCookies.ads_user_id;
697
+ cookies["ads_user_id"] = String(this.extraCookies.ads_user_id);
698
+ } else if (adsProfile?.id) {
699
+ cookies["ads_user_id"] = String(adsProfile.id);
645
700
  }
646
701
  } else if (domain === "page.ads.zalo.me") {
647
702
  await this.followOAuthRedirects("https://page.ads.zalo.me/api/login", cookies, false);
@@ -843,22 +898,31 @@ var ZaloSdkHttpClient = class {
843
898
  authByHost = /* @__PURE__ */ new Map();
844
899
  authManagerInstance;
845
900
  autoRetryOn401;
901
+ accountScope;
846
902
  constructor(options = {}) {
847
903
  this.fetchImpl = options.fetch ?? globalThis.fetch;
848
904
  if (!this.fetchImpl) throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
849
905
  this.baseUrls = { ...DEFAULT_BASE_URLS, ...options.baseUrls ?? {} };
850
906
  this.autoRetryOn401 = options.autoRetryOn401 ?? true;
907
+ this.accountScope = options.accountScope;
851
908
  if (options.authManager) {
852
909
  this.authManagerInstance = options.authManager;
910
+ if (options.accountScope && !this.authManagerInstance.getAccountScope()) {
911
+ this.authManagerInstance.setAccountScope(options.accountScope);
912
+ }
853
913
  } else if (options.zpsid || options.ssoAuth) {
854
914
  const extraCookies = {
855
915
  ...options.ssoAuth?.extraCookies ?? {},
856
916
  ...options.zoawSek ? { zoaw_sek: options.zoawSek } : {}
857
917
  };
918
+ if (options.accountScope?.memberId && String(options.accountScope.memberId) !== "0") {
919
+ extraCookies["ads_user_id"] = String(options.accountScope.memberId);
920
+ }
858
921
  this.authManagerInstance = new ZaloAuthManager({
859
922
  fetch: this.fetchImpl,
860
923
  integrationId: options.integrationId,
861
924
  accountKey: options.accountKey,
925
+ accountScope: options.accountScope ?? options.ssoAuth?.accountScope,
862
926
  sessionStore: options.sessionStore,
863
927
  redis: options.redis,
864
928
  redisKeyPrefix: options.redisKeyPrefix,
@@ -975,6 +1039,16 @@ var ZaloSdkHttpClient = class {
975
1039
  add("header_param", operation.headerFields);
976
1040
  add("cookie_param", operation.cookieFields);
977
1041
  add("body", operation.bodyFields);
1042
+ if (this.accountScope && output.query_param) {
1043
+ const query = output.query_param;
1044
+ const isAgencyContext = this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" || this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || Boolean(this.accountScope.agencyId);
1045
+ if (operation.queryFields.includes("agencyId") && query.agencyId === void 0 && isAgencyContext && this.accountScope.agencyId) {
1046
+ query.agencyId = Number(this.accountScope.agencyId);
1047
+ }
1048
+ if (operation.queryFields.includes("createdBy") && query.createdBy === void 0 && this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" && this.accountScope.memberId && String(this.accountScope.memberId) !== "0") {
1049
+ query.createdBy = Number(this.accountScope.memberId);
1050
+ }
1051
+ }
978
1052
  return output;
979
1053
  }
980
1054
  async resolveFile(input) {
@@ -3358,6 +3432,18 @@ var OP_ZaloAdsListAgencyCustomersWithBudget = {
3358
3432
  "cookieFields": [],
3359
3433
  "bodyFields": []
3360
3434
  };
3435
+ var OP_ZaloAdsGetApiBaAgencyEmployees = {
3436
+ "operationId": "ZaloAds_getApiBaAgencyEmployees",
3437
+ "method": "GET",
3438
+ "endpoint": "/api/ba/agency/employees",
3439
+ "baseUrl": "https://ads.zalo.me",
3440
+ "binaryFields": [],
3441
+ "pathFields": [],
3442
+ "queryFields": [],
3443
+ "headerFields": [],
3444
+ "cookieFields": [],
3445
+ "bodyFields": []
3446
+ };
3361
3447
  var OP_ZaloAdsGetApiNotifiesHeader = {
3362
3448
  "operationId": "ZaloAds_getApiNotifiesHeader",
3363
3449
  "method": "GET",
@@ -3407,6 +3493,9 @@ var ZaloAccountService = class {
3407
3493
  };
3408
3494
  return this.client.requestWithoutBody(OP_ZaloAdsListAgencyCustomersWithBudget, this.client.toTransportInput(OP_ZaloAdsListAgencyCustomersWithBudget, normalized), options);
3409
3495
  }
3496
+ getApiBaAgencyEmployees(request = {}, options) {
3497
+ return this.client.requestWithoutBody(OP_ZaloAdsGetApiBaAgencyEmployees, this.client.toTransportInput(OP_ZaloAdsGetApiBaAgencyEmployees, request), options);
3498
+ }
3410
3499
  getApiNotifiesHeader(request = {}, options) {
3411
3500
  return this.client.requestWithoutBody(OP_ZaloAdsGetApiNotifiesHeader, this.client.toTransportInput(OP_ZaloAdsGetApiNotifiesHeader, request), options);
3412
3501
  }
@@ -3429,6 +3518,10 @@ var ZaloAccountService = class {
3429
3518
  getBaAccountAllGrantedMembers(...args) {
3430
3519
  return this.getApiBaAccountAllgrantedmembers(...args);
3431
3520
  }
3521
+ /** Danh sách nhân viên trực thuộc Agency */
3522
+ getBaAgencyEmployees(...args) {
3523
+ return this.getApiBaAgencyEmployees(...args);
3524
+ }
3432
3525
  /** Thông báo trên thanh tiêu đề */
3433
3526
  getNotifiesHeader(...args) {
3434
3527
  return this.getApiNotifiesHeader(...args);
@@ -3862,7 +3955,6 @@ var ZaloReportsService = class {
3862
3955
  getReportapiV3Campaigns(request, options) {
3863
3956
  const normalized = {
3864
3957
  filter: "[]",
3865
- agencyId: 0,
3866
3958
  ...request
3867
3959
  };
3868
3960
  return this.client.requestWithoutBody(OP_ZaloAdsGetReportapiV3Campaigns, this.client.toTransportInput(OP_ZaloAdsGetReportapiV3Campaigns, normalized), options);
@@ -5170,6 +5262,12 @@ var ZaloAdsService = class extends ZaloCampaignsService {
5170
5262
  listAgencyCustomersWithBudget(request = { limit: 15, offset: 0, joinDateAsc: true }, options) {
5171
5263
  return this.account.listAgencyCustomersWithBudget(request, options);
5172
5264
  }
5265
+ getApiBaAgencyEmployees(request = {}, options) {
5266
+ return this.account.getApiBaAgencyEmployees(request, options);
5267
+ }
5268
+ getBaAgencyEmployees(...args) {
5269
+ return this.account.getBaAgencyEmployees(...args);
5270
+ }
5173
5271
  getApiNotifiesHeader(request = {}, options) {
5174
5272
  return this.account.getApiNotifiesHeader(request, options);
5175
5273
  }
@@ -7068,6 +7166,21 @@ var ZaloAdsSdk = class {
7068
7166
  get authManager() {
7069
7167
  return this.client.authManager;
7070
7168
  }
7169
+ /**
7170
+ * Phạm vi tài khoản quảng cáo cô lập (Isolated Workspace) đang gắn với instance SDK.
7171
+ */
7172
+ get accountScope() {
7173
+ return this.client.accountScope;
7174
+ }
7175
+ /**
7176
+ * Cập nhật phạm vi tài khoản quảng cáo cô lập.
7177
+ */
7178
+ setAccountScope(scope) {
7179
+ this.client.accountScope = scope;
7180
+ if (this.client.authManager) {
7181
+ this.client.authManager.setAccountScope(scope);
7182
+ }
7183
+ }
7071
7184
  /**
7072
7185
  * Cập nhật cookie `zpsid` gốc (hoặc chuỗi Cookie trình duyệt) để tự động xác thực cho toàn bộ các domain Zalo Ads.
7073
7186
  */
@@ -7147,6 +7260,9 @@ var ZaloAdsIntegrationManager = class {
7147
7260
  existing.sdk.setZpsid(request.zpsid);
7148
7261
  existing.zpsid = cleanZpsid;
7149
7262
  }
7263
+ if (request.accountScope) {
7264
+ existing.sdk.setAccountScope(request.accountScope);
7265
+ }
7150
7266
  this.sdkPool.set(integrationId, existing);
7151
7267
  return existing.sdk;
7152
7268
  }
@@ -7161,15 +7277,20 @@ var ZaloAdsIntegrationManager = class {
7161
7277
  ...request.extraCookies ?? {},
7162
7278
  ...request.zoawSek ? { zoaw_sek: request.zoawSek } : {}
7163
7279
  };
7280
+ if (request.accountScope?.memberId && String(request.accountScope.memberId) !== "0") {
7281
+ extraCookies.ads_user_id = String(request.accountScope.memberId);
7282
+ }
7164
7283
  const sdk = new ZaloAdsSdk({
7165
7284
  integrationId,
7166
7285
  zpsid: cleanZpsid,
7286
+ accountScope: request.accountScope,
7167
7287
  sessionStore: this.sessionStore,
7168
7288
  fetch: this.fetchImpl,
7169
7289
  baseUrls: this.baseUrls,
7170
7290
  ssoAuth: {
7171
7291
  integrationId,
7172
7292
  zpsid: cleanZpsid,
7293
+ accountScope: request.accountScope,
7173
7294
  extraCookies,
7174
7295
  sessionStore: this.sessionStore,
7175
7296
  sessionTtlMs: this.sessionTtlMs,
@@ -7253,7 +7374,8 @@ var ZaloAdsIntegrationManager = class {
7253
7374
  integrationId: accountKey,
7254
7375
  zpsid: params.zpsid,
7255
7376
  zoawSek: params.zoawSek,
7256
- extraCookies: params.extraCookies
7377
+ extraCookies: params.extraCookies,
7378
+ accountScope: params.accountScope
7257
7379
  });
7258
7380
  const sessions = await sdk.authenticateAll([
7259
7381
  "ads.zalo.me",