@@ -120,7 +120,7 @@ public function setSrvRecordAttribute(bool $isSrvRecord): void
120120 if ($ isSrvRecord ) {
121121 $ this ->attributes ['record_type ' ] = 'SRV ' ;
122122 } else {
123- $ ip = $ this ->server ->allocation ?->ip;
123+ $ ip = $ this ->server ? ->allocation?->ip;
124124 if (!empty ($ ip ) && filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 )) {
125125 $ this ->attributes ['record_type ' ] = 'AAAA ' ;
126126 } else {
@@ -136,9 +136,19 @@ protected function upsertOnCloudflare(): bool
136136 $ zoneId = $ this ->domain ->cloudflare_id ;
137137 $ domainName = $ this ->domain ->name ;
138138
139+ if ($ this ->server || $ this ->server ->node ) {
140+ Log::warning ('Subdomain server/node relation is null ' , ['subdomain_id ' => $ this ->id ]);
141+ Notification::make ()
142+ ->danger ()
143+ ->title (trans ('subdomains::strings.notifications.cloudflare_upsert_failed_title ' ))
144+ ->body (trans ('subdomains::strings.notifications.cloudflare_upsert_failed ' , ['subdomain ' => $ this ->getLabel () ?? 'unknown ' , 'errors ' => 'Server/node relation is null ' ]))
145+ ->send ();
146+
147+ return false ;
148+ }
149+
139150 if (empty ($ zoneId ) || empty ($ domainName )) {
140151 Log::warning ('Cloudflare zone id or name missing for domain ' , ['domain_id ' => $ this ->domain_id ]);
141-
142152 Notification::make ()
143153 ->danger ()
144154 ->title (trans ('subdomains::strings.notifications.cloudflare_missing_zone_title ' ))
@@ -192,7 +202,6 @@ protected function upsertOnCloudflare(): bool
192202 if ($ this ->exists ) {
193203 $ this ->updateQuietly (['cloudflare_id ' => $ result ['id ' ]]);
194204 } else {
195- // Model is being created, set attribute so it gets persisted with the insert
196205 $ this ->cloudflare_id = $ result ['id ' ];
197206 }
198207 }
@@ -225,7 +234,13 @@ protected function upsertOnCloudflare(): bool
225234 $ result = $ registrar ->upsertDnsRecord ($ zoneId , $ domainName , $ this ->name , $ this ->record_type , $ ip , $ this ->cloudflare_id , null , null );
226235
227236 if ($ result ['success ' ] && !empty ($ result ['id ' ])) {
228- $ this ->cloudflare_id = $ result ['id ' ];
237+ if ($ this ->cloudflare_id !== $ result ['id ' ]) {
238+ if ($ this ->exists ) {
239+ $ this ->updateQuietly (['cloudflare_id ' => $ result ['id ' ]]);
240+ } else {
241+ $ this ->cloudflare_id = $ result ['id ' ];
242+ }
243+ }
229244
230245 return true ;
231246 }
@@ -248,6 +263,17 @@ protected function deleteOnCloudflare(): bool
248263 return true ;
249264 }
250265
266+ if ($ this ->domain || empty ($ this ->domain ->cloudflare_id )) {
267+ Log::warning ('Cloudflare zone missing for subdomain during subdomain delete ' , ['domain_id ' => $ this ->domain_id ]);
268+ Notification::make ()
269+ ->danger ()
270+ ->title (trans ('subdomains::strings.notifications.cloudflare_delete_failed_title ' ))
271+ ->body (trans ('subdomains::strings.notifications.cloudflare_delete_failed ' , ['errors ' => 'Cloudflare zone missing for domain ' ]))
272+ ->send ();
273+
274+ return false ;
275+ }
276+
251277 $ registrar = app (CloudflareService::class);
252278
253279 $ result = $ registrar ->deleteDnsRecord ($ this ->domain ->cloudflare_id , $ this ->cloudflare_id );
0 commit comments