Skip to content

Commit

Permalink
fix: stopping fissa
Browse files Browse the repository at this point in the history
  • Loading branch information
xiduzo committed Jan 8, 2024
1 parent 5e05db3 commit 3bc0ba3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion apps/expo/app/fissa/[pin]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Fissa = () => {
if (!pin) return null;

return (
<PageTemplate fullScreen>
<PageTemplate fullScreen className="max-w-screen-2xl">
<Stack.Screen
options={{
headerShown: true,
Expand Down
5 changes: 1 addition & 4 deletions apps/nextjs/src/pages/api/cron/sync-fissa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma || new PrismaClient();

export default async function handler(_: NextApiRequest, res: NextApiResponse) {
const caller = appRouter.createCaller({
prisma,
session: null,
});
const caller = appRouter.createCaller({ prisma, session: null });

const fissas = await caller.fissa.sync.active();

Expand Down
4 changes: 0 additions & 4 deletions packages/api/src/router/fissa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export const fissaRouter = createTRPCRouter({
const service = new FissaService(ctx, new SpotifyService());
return service.byId(input, ctx.session.user.id);
}),
detailsById: protectedProcedure.input(Z_PIN).query(({ ctx, input }) => {
const service = new FissaService(ctx, new SpotifyService());
return service.detailsById(input);
}),
pause: protectedProcedure.input(Z_PIN).mutation(({ ctx, input }) => {
const service = new FissaService(ctx, new SpotifyService());
return service.pause(input, ctx.session.user.id);
Expand Down
36 changes: 12 additions & 24 deletions packages/api/src/service/FissaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,6 @@ export class FissaService extends ServiceWithContext {
return fissa;
};

detailsById = async (pin: string) => {
return this.db.fissa.findUnique({
where: { pin },
select: {
by: { select: { email: true } },
expectedEndTime: true,
currentlyPlayingId: true,
},
});
};

skipTrack = async (pin: string, userId: string) => {
const fissa = await this.byId(pin, userId);

Expand Down Expand Up @@ -155,9 +144,7 @@ export class FissaService extends ServiceWithContext {
try {
const isPlaying = await this.spotifyService.isStillPlaying(access_token);

if (!instantPlay && (!currentlyPlayingId || !isPlaying)) {
return this.stopFissa(pin, access_token);
}
if (!instantPlay && (!currentlyPlayingId || !isPlaying)) throw new Error("Stop fissa");

const nextTracks = this.getNextTracks(tracks, currentlyPlayingId);
if (!nextTracks[0]) throw new NoNextTrack();
Expand All @@ -179,11 +166,15 @@ export class FissaService extends ServiceWithContext {
};

private stopFissa = async (pin: string, accessToken: string) => {
await this.spotifyService.pause(accessToken);
return this.db.fissa.update({
where: { pin },
data: { currentlyPlaying: { disconnect: true } },
});
try {
await this.db.fissa.update({
where: { pin },
data: { currentlyPlaying: { disconnect: true } },
});
return this.spotifyService.pause(accessToken);
} catch (e) {
console.error(`${pin}, failed stopping fissa`, e);
}
};

private getFissaDetailedInformation = async (pin: string) => {
Expand Down Expand Up @@ -226,18 +217,15 @@ export class FissaService extends ServiceWithContext {
{ trackId, durationMs }: Pick<Track, "trackId" | "durationMs">,
accessToken: string,
) => {
console.log({ accessToken });
const promise = this.spotifyService.playTrack(accessToken, trackId);
await this.spotifyService.playTrack(accessToken, trackId);

await this.db.fissa.update({
return this.db.fissa.update({
where: { pin },
data: {
currentlyPlaying: { connect: { pin_trackId: { pin, trackId } } },
expectedEndTime: addMilliseconds(new Date(), durationMs),
},
});

return promise;
};

private getNextTracks = (tracks: Track[], currentlyPlayingId?: string | null) => {
Expand Down

1 comment on commit 3bc0ba3

@vercel
Copy link

@vercel vercel bot commented on 3bc0ba3 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.