@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.js CHANGED
@@ -451,6 +451,7 @@ function parseZaloCookieInput(rawInput) {
451
451
  }
452
452
  var ZaloAuthManager = class {
453
453
  explicitAccountKey;
454
+ accountScope;
454
455
  zpsid;
455
456
  getZpsidFn;
456
457
  extraCookies;
@@ -468,6 +469,7 @@ var ZaloAuthManager = class {
468
469
  throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
469
470
  }
470
471
  this.explicitAccountKey = normalizedOptions.integrationId ?? normalizedOptions.accountKey;
472
+ this.accountScope = normalizedOptions.accountScope;
471
473
  this.extraCookies = { ...normalizedOptions.extraCookies ?? {} };
472
474
  if (normalizedOptions.zpsid) {
473
475
  const parsed = parseZaloCookieInput(normalizedOptions.zpsid);
@@ -515,6 +517,12 @@ var ZaloAuthManager = class {
515
517
  getSessionStore() {
516
518
  return this.sessionStore;
517
519
  }
520
+ getAccountScope() {
521
+ return this.accountScope;
522
+ }
523
+ setAccountScope(scope) {
524
+ this.accountScope = scope;
525
+ }
518
526
  /**
519
527
  * Cập nhật `zpsid` mới và xóa toàn bộ cache phiên cũ trên cả RAM lẫn Redis.
520
528
  */
@@ -804,11 +812,58 @@ var ZaloAuthManager = class {
804
812
  const whoamiJson = await whoamiRes.json().catch(() => null);
805
813
  if (whoamiJson?.data) {
806
814
  adsProfile = whoamiJson.data;
807
- if (adsProfile.id) {
808
- cookies["ads_user_id"] = String(adsProfile.id);
815
+ }
816
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0" && !this.accountScope.isRoot && !explicitPlaySession) {
817
+ const memberIdStr = String(this.accountScope.memberId);
818
+ const switchUrls = [];
819
+ if (this.accountScope.accountType === "ACCESSOR") {
820
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
821
+ } else if (this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || this.accountScope.accountType === "AGENCY_SUB_ACCOUNT") {
822
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
823
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
824
+ } else {
825
+ switchUrls.push(`https://ads.zalo.me/api/ba/agency/login?memberId=${memberIdStr}`);
826
+ switchUrls.push(`https://ads.zalo.me/api/ba/personal/login?memberId=${memberIdStr}`);
827
+ }
828
+ for (const switchUrl of switchUrls) {
829
+ try {
830
+ const switchRes = await this.fetchImpl(switchUrl, {
831
+ method: "GET",
832
+ headers: {
833
+ Cookie: this.formatCookieHeader(cookies),
834
+ "User-Agent": this.userAgent,
835
+ Accept: "application/json, text/plain, */*"
836
+ },
837
+ redirect: "manual"
838
+ });
839
+ this.mergeSetCookies(switchRes, cookies);
840
+ const location = switchRes.headers.get("location") || "";
841
+ if (location && !location.includes("/client/denied")) {
842
+ const switchedWhoamiRes = await this.fetchImpl("https://ads.zalo.me/api/whoami", {
843
+ method: "GET",
844
+ headers: {
845
+ Cookie: this.formatCookieHeader(cookies),
846
+ "User-Agent": this.userAgent,
847
+ Accept: "application/json"
848
+ }
849
+ });
850
+ this.mergeSetCookies(switchedWhoamiRes, cookies);
851
+ const switchedJson = await switchedWhoamiRes.json().catch(() => null);
852
+ if (switchedJson?.data) {
853
+ adsProfile = switchedJson.data;
854
+ }
855
+ break;
856
+ }
857
+ } catch {
858
+ }
809
859
  }
860
+ }
861
+ if (this.accountScope?.memberId && String(this.accountScope.memberId) !== "0") {
862
+ cookies["ads_user_id"] = String(this.accountScope.memberId);
810
863
  } else if (this.extraCookies?.ads_user_id) {
811
- cookies["ads_user_id"] = this.extraCookies.ads_user_id;
864
+ cookies["ads_user_id"] = String(this.extraCookies.ads_user_id);
865
+ } else if (adsProfile?.id) {
866
+ cookies["ads_user_id"] = String(adsProfile.id);
812
867
  }
813
868
  } else if (domain === "page.ads.zalo.me") {
814
869
  await this.followOAuthRedirects("https://page.ads.zalo.me/api/login", cookies, false);
@@ -1010,22 +1065,31 @@ var ZaloSdkHttpClient = class {
1010
1065
  authByHost = /* @__PURE__ */ new Map();
1011
1066
  authManagerInstance;
1012
1067
  autoRetryOn401;
1068
+ accountScope;
1013
1069
  constructor(options = {}) {
1014
1070
  this.fetchImpl = options.fetch ?? globalThis.fetch;
1015
1071
  if (!this.fetchImpl) throw new Error("A Fetch implementation is required (Node.js 18+ provides global fetch).");
1016
1072
  this.baseUrls = { ...DEFAULT_BASE_URLS, ...options.baseUrls ?? {} };
1017
1073
  this.autoRetryOn401 = options.autoRetryOn401 ?? true;
1074
+ this.accountScope = options.accountScope;
1018
1075
  if (options.authManager) {
1019
1076
  this.authManagerInstance = options.authManager;
1077
+ if (options.accountScope && !this.authManagerInstance.getAccountScope()) {
1078
+ this.authManagerInstance.setAccountScope(options.accountScope);
1079
+ }
1020
1080
  } else if (options.zpsid || options.ssoAuth) {
1021
1081
  const extraCookies = {
1022
1082
  ...options.ssoAuth?.extraCookies ?? {},
1023
1083
  ...options.zoawSek ? { zoaw_sek: options.zoawSek } : {}
1024
1084
  };
1085
+ if (options.accountScope?.memberId && String(options.accountScope.memberId) !== "0") {
1086
+ extraCookies["ads_user_id"] = String(options.accountScope.memberId);
1087
+ }
1025
1088
  this.authManagerInstance = new ZaloAuthManager({
1026
1089
  fetch: this.fetchImpl,
1027
1090
  integrationId: options.integrationId,
1028
1091
  accountKey: options.accountKey,
1092
+ accountScope: options.accountScope ?? options.ssoAuth?.accountScope,
1029
1093
  sessionStore: options.sessionStore,
1030
1094
  redis: options.redis,
1031
1095
  redisKeyPrefix: options.redisKeyPrefix,
@@ -1142,6 +1206,16 @@ var ZaloSdkHttpClient = class {
1142
1206
  add("header_param", operation.headerFields);
1143
1207
  add("cookie_param", operation.cookieFields);
1144
1208
  add("body", operation.bodyFields);
1209
+ if (this.accountScope && output.query_param) {
1210
+ const query = output.query_param;
1211
+ const isAgencyContext = this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" || this.accountScope.accountType === "AGENCY_MASTER" || this.accountScope.accountType === "AGENCY" || Boolean(this.accountScope.agencyId);
1212
+ if (operation.queryFields.includes("agencyId") && query.agencyId === void 0 && isAgencyContext && this.accountScope.agencyId) {
1213
+ query.agencyId = Number(this.accountScope.agencyId);
1214
+ }
1215
+ if (operation.queryFields.includes("createdBy") && query.createdBy === void 0 && this.accountScope.accountType === "AGENCY_SUB_ACCOUNT" && this.accountScope.memberId && String(this.accountScope.memberId) !== "0") {
1216
+ query.createdBy = Number(this.accountScope.memberId);
1217
+ }
1218
+ }
1145
1219
  return output;
1146
1220
  }
1147
1221
  async resolveFile(input) {
@@ -3525,6 +3599,18 @@ var OP_ZaloAdsListAgencyCustomersWithBudget = {
3525
3599
  "cookieFields": [],
3526
3600
  "bodyFields": []
3527
3601
  };
3602
+ var OP_ZaloAdsGetApiBaAgencyEmployees = {
3603
+ "operationId": "ZaloAds_getApiBaAgencyEmployees",
3604
+ "method": "GET",
3605
+ "endpoint": "/api/ba/agency/employees",
3606
+ "baseUrl": "https://ads.zalo.me",
3607
+ "binaryFields": [],
3608
+ "pathFields": [],
3609
+ "queryFields": [],
3610
+ "headerFields": [],
3611
+ "cookieFields": [],
3612
+ "bodyFields": []
3613
+ };
3528
3614
  var OP_ZaloAdsGetApiNotifiesHeader = {
3529
3615
  "operationId": "ZaloAds_getApiNotifiesHeader",
3530
3616
  "method": "GET",
@@ -3574,6 +3660,9 @@ var ZaloAccountService = class {
3574
3660
  };
3575
3661
  return this.client.requestWithoutBody(OP_ZaloAdsListAgencyCustomersWithBudget, this.client.toTransportInput(OP_ZaloAdsListAgencyCustomersWithBudget, normalized), options);
3576
3662
  }
3663
+ getApiBaAgencyEmployees(request = {}, options) {
3664
+ return this.client.requestWithoutBody(OP_ZaloAdsGetApiBaAgencyEmployees, this.client.toTransportInput(OP_ZaloAdsGetApiBaAgencyEmployees, request), options);
3665
+ }
3577
3666
  getApiNotifiesHeader(request = {}, options) {
3578
3667
  return this.client.requestWithoutBody(OP_ZaloAdsGetApiNotifiesHeader, this.client.toTransportInput(OP_ZaloAdsGetApiNotifiesHeader, request), options);
3579
3668
  }
@@ -3596,6 +3685,10 @@ var ZaloAccountService = class {
3596
3685
  getBaAccountAllGrantedMembers(...args) {
3597
3686
  return this.getApiBaAccountAllgrantedmembers(...args);
3598
3687
  }
3688
+ /** Danh sách nhân viên trực thuộc Agency */
3689
+ getBaAgencyEmployees(...args) {
3690
+ return this.getApiBaAgencyEmployees(...args);
3691
+ }
3599
3692
  /** Thông báo trên thanh tiêu đề */
3600
3693
  getNotifiesHeader(...args) {
3601
3694
  return this.getApiNotifiesHeader(...args);
@@ -4029,7 +4122,6 @@ var ZaloReportsService = class {
4029
4122
  getReportapiV3Campaigns(request, options) {
4030
4123
  const normalized = {
4031
4124
  filter: "[]",
4032
- agencyId: 0,
4033
4125
  ...request
4034
4126
  };
4035
4127
  return this.client.requestWithoutBody(OP_ZaloAdsGetReportapiV3Campaigns, this.client.toTransportInput(OP_ZaloAdsGetReportapiV3Campaigns, normalized), options);
@@ -5337,6 +5429,12 @@ var ZaloAdsService = class extends ZaloCampaignsService {
5337
5429
  listAgencyCustomersWithBudget(request = { limit: 15, offset: 0, joinDateAsc: true }, options) {
5338
5430
  return this.account.listAgencyCustomersWithBudget(request, options);
5339
5431
  }
5432
+ getApiBaAgencyEmployees(request = {}, options) {
5433
+ return this.account.getApiBaAgencyEmployees(request, options);
5434
+ }
5435
+ getBaAgencyEmployees(...args) {
5436
+ return this.account.getBaAgencyEmployees(...args);
5437
+ }
5340
5438
  getApiNotifiesHeader(request = {}, options) {
5341
5439
  return this.account.getApiNotifiesHeader(request, options);
5342
5440
  }
@@ -7235,6 +7333,21 @@ var ZaloAdsSdk = class {
7235
7333
  get authManager() {
7236
7334
  return this.client.authManager;
7237
7335
  }
7336
+ /**
7337
+ * Phạm vi tài khoản quảng cáo cô lập (Isolated Workspace) đang gắn với instance SDK.
7338
+ */
7339
+ get accountScope() {
7340
+ return this.client.accountScope;
7341
+ }
7342
+ /**
7343
+ * Cập nhật phạm vi tài khoản quảng cáo cô lập.
7344
+ */
7345
+ setAccountScope(scope) {
7346
+ this.client.accountScope = scope;
7347
+ if (this.client.authManager) {
7348
+ this.client.authManager.setAccountScope(scope);
7349
+ }
7350
+ }
7238
7351
  /**
7239
7352
  * 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.
7240
7353
  */
@@ -7314,6 +7427,9 @@ var ZaloAdsIntegrationManager = class {
7314
7427
  existing.sdk.setZpsid(request.zpsid);
7315
7428
  existing.zpsid = cleanZpsid;
7316
7429
  }
7430
+ if (request.accountScope) {
7431
+ existing.sdk.setAccountScope(request.accountScope);
7432
+ }
7317
7433
  this.sdkPool.set(integrationId, existing);
7318
7434
  return existing.sdk;
7319
7435
  }
@@ -7328,15 +7444,20 @@ var ZaloAdsIntegrationManager = class {
7328
7444
  ...request.extraCookies ?? {},
7329
7445
  ...request.zoawSek ? { zoaw_sek: request.zoawSek } : {}
7330
7446
  };
7447
+ if (request.accountScope?.memberId && String(request.accountScope.memberId) !== "0") {
7448
+ extraCookies.ads_user_id = String(request.accountScope.memberId);
7449
+ }
7331
7450
  const sdk = new ZaloAdsSdk({
7332
7451
  integrationId,
7333
7452
  zpsid: cleanZpsid,
7453
+ accountScope: request.accountScope,
7334
7454
  sessionStore: this.sessionStore,
7335
7455
  fetch: this.fetchImpl,
7336
7456
  baseUrls: this.baseUrls,
7337
7457
  ssoAuth: {
7338
7458
  integrationId,
7339
7459
  zpsid: cleanZpsid,
7460
+ accountScope: request.accountScope,
7340
7461
  extraCookies,
7341
7462
  sessionStore: this.sessionStore,
7342
7463
  sessionTtlMs: this.sessionTtlMs,
@@ -7420,7 +7541,8 @@ var ZaloAdsIntegrationManager = class {
7420
7541
  integrationId: accountKey,
7421
7542
  zpsid: params.zpsid,
7422
7543
  zoawSek: params.zoawSek,
7423
- extraCookies: params.extraCookies
7544
+ extraCookies: params.extraCookies,
7545
+ accountScope: params.accountScope
7424
7546
  });
7425
7547
  const sessions = await sdk.authenticateAll([
7426
7548
  "ads.zalo.me",