()
| 134 | .mutation(async ({ input, ctx }) => |
| 135 | protectedMutationFetchFirst({ |
| 136 | async operation() { |
| 137 | const { serverId, customDomain: newCustomDomain } = input; |
| 138 | try { |
| 139 | const oldServerInfo = await findServerById(serverId); |
| 140 | if (!oldServerInfo) { |
| 141 | throw new TRPCError({ |
| 142 | code: 'NOT_FOUND', |
| 143 | message: 'Server not found', |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | const isValidDomain = |
| 148 | validDomainRegex.test(newCustomDomain) || newCustomDomain === ''; |
| 149 | if (!isValidDomain) { |
| 150 | throw new TRPCError({ |
| 151 | code: 'BAD_REQUEST', |
| 152 | message: 'Invalid domain', |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | if (newCustomDomain.toLowerCase().endsWith('.answeroverflow.com')) { |
| 157 | throw new TRPCError({ |
| 158 | code: 'BAD_REQUEST', |
| 159 | message: |
| 160 | 'Domain cannot end with .answeroverflow.com. Please use a domain that you own', |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | const response = await updateServer({ |
| 165 | existing: oldServerInfo, |
| 166 | update: { |
| 167 | customDomain: newCustomDomain === '' ? null : newCustomDomain, |
| 168 | id: serverId, |
| 169 | }, |
| 170 | }); |
| 171 | await addDomainToVercel(newCustomDomain); |
| 172 | |
| 173 | // if the site had a different customDomain before, we need to remove it from Vercel |
| 174 | if ( |
| 175 | oldServerInfo.customDomain && |
| 176 | oldServerInfo.customDomain !== newCustomDomain |
| 177 | ) { |
| 178 | await removeDomainFromVercelProject(oldServerInfo.customDomain); |
| 179 | } |
| 180 | |
| 181 | // TODO: Update cache? |
| 182 | |
| 183 | return response; |
| 184 | } catch (error: any) { |
| 185 | if ( |
| 186 | 'code' in error && |
| 187 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 188 | error.code === 'P2002' |
| 189 | ) { |
| 190 | throw new TRPCError({ |
| 191 | code: 'BAD_REQUEST', |
| 192 | message: `Domain ${newCustomDomain} already in use`, |
| 193 | }); |
no test coverage detected