harmonyV

goaffpro.ts

Nov 4th, 2025 (edited)
924
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface GoAffProAffiliate {
  2.     id: number;
  3.     email: string;
  4.     ref_code: string;
  5.     status: string;
  6.     created_at: string;
  7.     updated_at: string;
  8. }
  9.  
  10. interface CreateAffiliateRequest {
  11.     email: string;
  12.     first_name?: string;
  13.     last_name?: string;
  14.     name: string;
  15.     password: string;
  16. }
  17.  
  18. interface CreateAffiliateResponse {
  19.     success: boolean;
  20.     affiliate: GoAffProAffiliate | null;
  21.     message?: string;
  22.     errors?: Record<string, string[]>;
  23. }
  24.  
  25. interface UpdateAffiliateResponse {
  26.     success: boolean;
  27.     data?: {
  28.         affiliate?: string | Record<string, unknown>;
  29.         updated?: boolean;
  30.         id?: number;
  31.     };
  32.     error?: string;
  33. }
  34.  
  35. /**
  36.  * Helper function to update an affiliate via PATCH
  37.  */
  38. async function updateGoAffProAffiliate(
  39.     id: number,
  40.     body: Record<string, unknown>,
  41. ): Promise<UpdateAffiliateResponse> {
  42.     try {
  43.         const response = await fetch(
  44.             `${serverConfig.goaffproApiUrl}/admin/affiliates/${id}`,
  45.             {
  46.                 method: "PATCH",
  47.                 headers: {
  48.                     "Content-Type": "application/json",
  49.                     "x-goaffpro-access-token": serverConfig.goaffproApiKey,
  50.                 },
  51.                 body: JSON.stringify(body),
  52.             },
  53.         );
  54.  
  55.         const responseData = await response.json();
  56.  
  57.         if (!response.ok) {
  58.             return {
  59.                 success: false,
  60.                 error: responseData.error || responseData.message ||
  61.                     "Failed to update affiliate",
  62.             };
  63.         }
  64.  
  65.         return {
  66.             success: true,
  67.             data: responseData,
  68.         };
  69.     } catch (error) {
  70.         console.error("Error updating GoAffPro affiliate:", error);
  71.  
  72.         return {
  73.             success: false,
  74.             error: "Internal server error",
  75.         };
  76.     }
  77. }
  78.  
  79. /**
  80.  * Create a new affiliate in GoAffPro
  81.  */
  82. export async function createGoAffProAffiliate(
  83.     affiliateData: CreateAffiliateRequest,
  84. ): Promise<CreateAffiliateResponse> {
  85.     console.log("affiliateData", affiliateData);
  86.     try {
  87.         const response = await fetch(
  88.             `${serverConfig.goaffproApiUrl}/admin/affiliates`,
  89.             {
  90.                 method: "POST",
  91.                 headers: {
  92.                     "Content-Type": "application/json",
  93.                     "x-goaffpro-access-token": serverConfig.goaffproApiKey,
  94.                 },
  95.                 body: JSON.stringify({
  96.                     email: affiliateData.email,
  97.                     name: affiliateData.name,
  98.                     password: affiliateData.password,
  99.                     password_expired: true,
  100.                     status: "pending",
  101.                     first_name: affiliateData.first_name,
  102.                     last_name: affiliateData.last_name,
  103.                 }),
  104.             },
  105.         );
  106.  
  107.         const responseData = await response.json();
  108.  
  109.         if (!response.ok) {
  110.             console.error("Error creating GoAffPro affiliate:", responseData);
  111.             return {
  112.                 success: false,
  113.                 affiliate: null,
  114.                 message: responseData.error || "Failed to create affiliate",
  115.                 errors: responseData.errors,
  116.             };
  117.         }
  118.  
  119.         // Get the affiliate ID from the response
  120.         const createdAffiliateId = responseData.affiliate_id;
  121.  
  122.         // Update name fields immediately after creation
  123.         // This is necessary because the creation endpoint doesn't always persist these fields
  124.         if (
  125.             createdAffiliateId &&
  126.             (affiliateData.name || affiliateData.first_name ||
  127.                 affiliateData.last_name)
  128.         ) {
  129.             const updateResult = await updateGoAffProAffiliate(
  130.                 createdAffiliateId,
  131.                 {
  132.                     ...(affiliateData.name && { name: affiliateData.name }),
  133.                     ...(affiliateData.first_name &&
  134.                         { first_name: affiliateData.first_name }),
  135.                     ...(affiliateData.last_name &&
  136.                         { last_name: affiliateData.last_name }),
  137.                 },
  138.             );
  139.  
  140.             if (!updateResult.success) {
  141.                 console.warn(
  142.                     "Failed to update affiliate name fields:",
  143.                     updateResult.error,
  144.                 );
  145.                 // Don't fail the entire creation if update fails
  146.             }
  147.         }
  148.  
  149.         const affiliate = await getGoAffProAffiliateByEmail(
  150.             affiliateData.email,
  151.         );
  152.  
  153.         return {
  154.             success: true,
  155.             affiliate: affiliate,
  156.             message: "Affiliate created successfully",
  157.         };
  158.     } catch (error) {
  159.         console.error("Error creating GoAffPro affiliate:", error);
  160.         return {
  161.             success: false,
  162.             affiliate: null,
  163.             message: "Internal server error",
  164.         };
  165.     }
  166. }
  167.  
  168. /**
  169.  * Approve affiliate by id from GoAffPro
  170.  */
  171. export async function approveGoAffProAffiliate(
  172.     id: number,
  173. ): Promise<
  174.     { success: boolean; message?: string; error?: string; id?: number }
  175. > {
  176.     try {
  177.         const updateResult = await updateGoAffProAffiliate(id, {
  178.             status: "approved",
  179.             first_name: "David",
  180.             last_name: "Peter",
  181.         });
  182.  
  183.         if (!updateResult.success) {
  184.             return {
  185.                 success: false,
  186.                 message: updateResult.error ||
  187.                     "Failed to approve affiliate in GoAffPro",
  188.             };
  189.         }
  190.  
  191.         // Parse affiliate if it's a JSON string
  192.         const affiliateData = updateResult.data?.affiliate;
  193.         const affiliate = typeof affiliateData === "string"
  194.             ? JSON.parse(affiliateData)
  195.             : affiliateData;
  196.  
  197.         const success = affiliate?.status === "approved" &&
  198.             updateResult.data?.updated === true &&
  199.             updateResult.data?.id != null;
  200.  
  201.         console.log("success", success);
  202.  
  203.         if (!success) {
  204.             return {
  205.                 success: false,
  206.                 message: "Failed to approve affiliate in GoAffPro",
  207.             };
  208.         }
  209.  
  210.         return {
  211.             success: true,
  212.             message: "Affiliate approved successfully",
  213.             id: updateResult.data?.id,
  214.         };
  215.     } catch (error) {
  216.         console.error("Error approving GoAffPro affiliate:", error);
  217.         return { success: false, message: "Internal server error" };
  218.     }
  219. }
Advertisement
Comments
  • wmgubuw
    2 days
    # text 0.12 KB | 0 0
    1. ⭐Make 3000$ with Swapzone Exchange Method
    2.  
    3. >>> docs.google.com/document/d/1IB4SkLZdU8hex0Kn-GyFHXSSV6kLUXhhOhxPwmEuuc4
Add Comment
Please, Sign In to add comment