Skip to content

Commit

Permalink
fix: Incorrect initial state for new rules
Browse files Browse the repository at this point in the history
- also remove the default server option for *arr settings as this is no longer used.
  • Loading branch information
benscobie committed Dec 6, 2024
1 parent 80026ff commit 439cf9f
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 238 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class RemoveDefaultArrServers1733357722729
implements MigrationInterface
{
name = 'RemoveDefaultArrServers1733357722729';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "temporary_sonarr_settings" (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"serverName" varchar NOT NULL,
"url" varchar,
"apiKey" varchar
)
`);
await queryRunner.query(`
INSERT INTO "temporary_sonarr_settings"("id", "serverName", "url", "apiKey")
SELECT "id",
"serverName",
"url",
"apiKey"
FROM "sonarr_settings"
`);
await queryRunner.query(`
DROP TABLE "sonarr_settings"
`);
await queryRunner.query(`
ALTER TABLE "temporary_sonarr_settings"
RENAME TO "sonarr_settings"
`);
await queryRunner.query(`
CREATE TABLE "temporary_radarr_settings" (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"serverName" varchar NOT NULL,
"url" varchar,
"apiKey" varchar
)
`);
await queryRunner.query(`
INSERT INTO "temporary_radarr_settings"("id", "serverName", "url", "apiKey")
SELECT "id",
"serverName",
"url",
"apiKey"
FROM "radarr_settings"
`);
await queryRunner.query(`
DROP TABLE "radarr_settings"
`);
await queryRunner.query(`
ALTER TABLE "temporary_radarr_settings"
RENAME TO "radarr_settings"
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
6 changes: 3 additions & 3 deletions server/src/modules/api/plex-api/plex-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,13 @@ export class PlexApiService {
}

public async deleteMediaFromDisk(plexId: number | string): Promise<void> {
this.logger.log(
`[Plex] Removed media with ID ${plexId} from Plex library.`,
);
try {
await this.plexClient.deleteQuery({
uri: `/library/metadata/${plexId}`,
});
this.logger.log(
`[Plex] Removed media with ID ${plexId} from Plex library.`,
);
} catch (e) {
this.logger.warn('Something went wrong while removing media from Plex.', {
label: 'Plex API',
Expand Down
2 changes: 0 additions & 2 deletions server/src/modules/settings/dto's/radarr-setting.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export type RadarrSettingDto = {
url: string;

apiKey: string;

isDefault: boolean;
};

export type RadarrSettingRawDto = Omit<RadarrSettingDto, 'id'>;
Expand Down
2 changes: 0 additions & 2 deletions server/src/modules/settings/dto's/sonarr-setting.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export type SonarrSettingDto = {
url: string;

apiKey: string;

isDefault: boolean;
};

export type SonarrSettingRawDto = Omit<SonarrSettingDto, 'id'>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export class RadarrSettings {
@Column({ nullable: true })
apiKey: string;

@Column({ default: false })
isDefault: boolean;

@OneToMany(() => Collection, (collection) => collection.radarrSettings)
collections: Collection[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export class SonarrSettings {
@Column({ nullable: true })
apiKey: string;

@Column({ default: false })
isDefault: boolean;

@OneToMany(() => Collection, (collection) => collection.sonarrSettings)
collections: Collection[];
}
124 changes: 35 additions & 89 deletions server/src/modules/settings/settings.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { randomUUID } from 'crypto';
import { Not, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import { isValidCron } from 'cron-validator';
import { BasicResponseDto } from '../api/external-api/dto/basic-response.dto';
import { OverseerrApiService } from '../api/overseerr-api/overseerr-api.service';
Expand Down Expand Up @@ -154,25 +154,15 @@ export class SettingsService implements SettingDto {
try {
settings.url = settings.url.toLowerCase();

return this.radarrSettingsRepo.manager.transaction(async (manager) => {
if (settings.isDefault) {
await manager
.withRepository(this.radarrSettingsRepo)
.update({ isDefault: true }, { isDefault: false });
}

const savedSetting = await manager
.withRepository(this.radarrSettingsRepo)
.save(settings);
const savedSetting = await this.radarrSettingsRepo.save(settings);

this.logger.log('Radarr setting added');
return {
data: savedSetting,
status: 'OK',
code: 1,
message: 'Success',
};
});
this.logger.log('Radarr setting added');
return {
data: savedSetting,
status: 'OK',
code: 1,
message: 'Success',
};
} catch (e) {
this.logger.error('Error while adding Radarr setting: ', e);
return { status: 'NOK', code: 0, message: 'Failure' };
Expand All @@ -185,33 +175,16 @@ export class SettingsService implements SettingDto {
try {
settings.url = settings.url.toLowerCase();

const data = await this.radarrSettingsRepo.manager.transaction(
async (manager) => {
const settingsDb = await manager
.withRepository(this.radarrSettingsRepo)
.findOne({
where: { id: settings.id },
});

const data = {
...settingsDb,
...settings,
};

await manager.withRepository(this.radarrSettingsRepo).save(data);

if (settings.isDefault) {
await manager
.withRepository(this.radarrSettingsRepo)
.update(
{ isDefault: true, id: Not(settings.id) },
{ isDefault: false },
);
}
const settingsDb = await this.radarrSettingsRepo.findOne({
where: { id: settings.id },
});

return data;
},
);
const data = {
...settingsDb,
...settings,
};

await this.radarrSettingsRepo.save(data);

this.servarr.deleteCachedRadarrApiClient(settings.id);
this.logger.log('Radarr settings updated');
Expand Down Expand Up @@ -284,25 +257,15 @@ export class SettingsService implements SettingDto {
try {
settings.url = settings.url.toLowerCase();

return this.sonarrSettingsRepo.manager.transaction(async (manager) => {
if (settings.isDefault) {
await manager
.withRepository(this.sonarrSettingsRepo)
.update({ isDefault: true }, { isDefault: false });
}

const savedSetting = await manager
.withRepository(this.sonarrSettingsRepo)
.save(settings);
const savedSetting = await this.sonarrSettingsRepo.save(settings);

this.logger.log('Sonarr setting added');
return {
data: savedSetting,
status: 'OK',
code: 1,
message: 'Success',
};
});
this.logger.log('Sonarr setting added');
return {
data: savedSetting,
status: 'OK',
code: 1,
message: 'Success',
};
} catch (e) {
this.logger.error('Error while adding Sonarr setting: ', e);
return { status: 'NOK', code: 0, message: 'Failure' };
Expand All @@ -315,33 +278,16 @@ export class SettingsService implements SettingDto {
try {
settings.url = settings.url.toLowerCase();

const data = await this.sonarrSettingsRepo.manager.transaction(
async (manager) => {
const settingsDb = await manager
.withRepository(this.sonarrSettingsRepo)
.findOne({
where: { id: settings.id },
});

const data = {
...settingsDb,
...settings,
};

await manager.withRepository(this.sonarrSettingsRepo).save(data);

if (settings.isDefault) {
await manager
.withRepository(this.sonarrSettingsRepo)
.update(
{ isDefault: true, id: Not(settings.id) },
{ isDefault: false },
);
}
const settingsDb = await this.sonarrSettingsRepo.findOne({
where: { id: settings.id },
});

return data;
},
);
const data = {
...settingsDb,
...settings,
};

await this.sonarrSettingsRepo.save(data);

this.servarr.deleteCachedSonarrApiClient(settings.id);

Expand Down
42 changes: 20 additions & 22 deletions ui/src/components/Rules/RuleGroup/AddModal/ArrAction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type ArrType = 'Radarr' | 'Sonarr'
interface ArrActionProps {
type: ArrType
arrAction?: number
settingId?: number
settingId?: number | null // null for when the user has selected 'None', undefined for when this is a new rule
options?: Option[]
onUpdate: (value: number, settingId?: number) => void
onUpdate: (value: number, settingId?: number | null) => void
}

interface Option {
Expand All @@ -19,14 +19,15 @@ interface Option {
}

const ArrAction = (props: ArrActionProps) => {
const [prevType, setPrevType] = useState<ArrType>(props.type)
const selectedSetting =
props.settingId === undefined ? '-1' : (props.settingId?.toString() ?? '')
const [settings, setSettings] = useState<(IRadarrSetting | ISonarrSetting)[]>(
[],
)
const [loading, setLoading] = useState<boolean>(true)
const action = props.arrAction ? props.arrAction : 0

const handleSelectedSettingIdChange = (id?: number) => {
const handleSelectedSettingIdChange = (id?: number | null) => {
props.onUpdate(action, id)
}

Expand All @@ -40,19 +41,15 @@ const ArrAction = (props: ArrActionProps) => {
const settingsResponse = await GetApiHandler<IRadarrSetting[]>(
`/settings/${type.toLowerCase()}`,
)
setPrevType(type)
setSettings(settingsResponse)
setLoading(false)

if (!props.settingId || type != prevType) {
if (settingsResponse.length > 0) {
const defaultServer = settingsResponse.find((s) => s.isDefault)
handleSelectedSettingIdChange(
defaultServer ? defaultServer.id : settingsResponse[0]?.id,
)
} else {
handleSelectedSettingIdChange(undefined)
}
// The selected server does not exist anymore (old client data potentially) so deselect
if (
props.settingId &&
settingsResponse.find((x) => x.id === props.settingId) == null
) {
handleSelectedSettingIdChange(undefined)
}
}

Expand Down Expand Up @@ -81,30 +78,31 @@ const ArrAction = (props: ArrActionProps) => {
<div>
<div className="form-row">
<label htmlFor={`${props.type}-server`} className="text-label">
{props.type} server
{props.type} server *
</label>
<div className="form-input">
<div className="form-input-field">
<select
name={`${props.type}-server`}
id={`${props.type}-server`}
value={props.settingId}
value={selectedSetting}
onChange={(e) => {
handleSelectedSettingIdChange(+e.target.value)
handleSelectedSettingIdChange(
e.target.value == '' ? null : +e.target.value,
)
}}
>
{selectedSetting === '-1' && (
<option value="-1" disabled></option>
)}
<option value="">None</option>
{settings.map((e) => {
return (
<option key={e.id} value={e.id}>
{e.serverName}
</option>
)
})}
{settings.length === 0 && (
<option value="" disabled>
No servers added
</option>
)}
{loading && (
<option value="" disabled>
Loading servers...
Expand Down
Loading

0 comments on commit 439cf9f

Please sign in to comment.