Skip to content

Commit

Permalink
refactor api services
Browse files Browse the repository at this point in the history
  • Loading branch information
xiduzo committed Jan 4, 2024
1 parent 507ac9c commit fc6cd39
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 188 deletions.
10 changes: 5 additions & 5 deletions packages/api/src/router/fissa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ const sync = createTRPCRouter({
export const fissaRouter = createTRPCRouter({
skipTrack: protectedProcedure.input(Z_PIN).mutation(({ ctx, input }) => {
const service = new FissaService(ctx, new SpotifyService());
return service.skipTrack(input);
return service.skipTrack(input, ctx.session.user.id);
}),
restart: protectedProcedure.input(Z_PIN).mutation(({ ctx, input }) => {
const service = new FissaService(ctx, new SpotifyService());
return service.restart(input);
return service.restart(input, ctx.session.user.id);
}),
create: protectedProcedure.input(Z_TRACKS).mutation(({ ctx, input }) => {
const service = new FissaService(ctx, new SpotifyService());
return service.create(input);
return service.create(input, ctx.session.user.id);
}),
byId: protectedProcedure.input(Z_PIN).query(({ ctx, input }) => {
const service = new FissaService(ctx, new SpotifyService());
return service.byId(input);
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);
return service.pause(input, ctx.session.user.id);
}),
sync,
});
2 changes: 1 addition & 1 deletion packages/api/src/router/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const trackRouter = createTRPCRouter({
addTracks: protectedProcedure.input(addTracks).mutation(async ({ ctx, input }) => {
const service = new TrackService(ctx, new VoteService(ctx));

await service.addTracks(input.pin, input.tracks);
await service.addTracks(input.pin, input.tracks, ctx.session.user.id);
}),
deleteTrack: protectedProcedure.input(deleteTrack).mutation(async ({ ctx, input }) => {
const service = new TrackService(ctx, new VoteService(ctx));
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/router/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export const voteRouter = createTRPCRouter({
}),
byTrackFromUser: protectedProcedure.input(vote).query(({ ctx, input }) => {
const service = new VoteService(ctx);
return service.getVoteFromUser(input.pin, input.trackId);
return service.getVoteFromUser(input.pin, input.trackId, ctx.session.user.id);
}),
create: protectedProcedure.input(createVote).mutation(async ({ ctx, input }) => {
const service = new VoteService(ctx);
return service.createVote(input.pin, input.trackId, input.vote);
return service.createVote(input.pin, input.trackId, input.vote, ctx.session.user.id);
}),
});
34 changes: 11 additions & 23 deletions packages/api/src/service/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ export class AuthService extends ServiceWithContext {
}

getUserFissa = async () => {
const { hostOf, isIn } = await this.db.user.findUniqueOrThrow({
return this.db.user.findUniqueOrThrow({
where: { id: this.ctx.session?.user.id },
select: {
hostOf: { select: { pin: true } },
isIn: { select: { pin: true }, orderBy: { createdAt: Prisma.SortOrder.desc } },
},
});

return {
hostOf,
isIn,
};
};

getAccessToken = async (code: string, redirectUri: string) => {
Expand All @@ -41,18 +36,16 @@ export class AuthService extends ServiceWithContext {
},
};

let existingUser = await this.db.user.findUnique(findUser);
let user = await this.db.user.findUnique(findUser);

if (!existingUser) {
await this.createUser(spotifyUser.body, tokens);
existingUser = await this.db.user.findUniqueOrThrow(findUser);
if (!user) {
await this.createAccount(spotifyUser.body, tokens);
user = await this.db.user.findUniqueOrThrow(findUser);
}

if (!existingUser.sessions[0]) return tokens.body;

return {
...tokens.body,
session_token: existingUser.sessions[0].sessionToken,
session_token: user?.sessions[0]?.sessionToken,
};
};

Expand Down Expand Up @@ -87,11 +80,9 @@ export class AuthService extends ServiceWithContext {
},
});

if (!session) return tokens.body;

return {
...tokens.body,
session_token: session.sessionToken,
session_token: session?.sessionToken,
};
};

Expand All @@ -111,13 +102,10 @@ export class AuthService extends ServiceWithContext {
},
});

const { lastUpdateAt } = fissa;

if (differenceInMinutes(lastUpdateAt, new Date()) > 20) return;
if (differenceInMinutes(fissa.lastUpdateAt, new Date()) > 20) return;

const { accounts } = fissa.by;
if (!accounts[0]) return;
const { expires_at, refresh_token } = accounts[0];
if (!fissa.by.accounts[0]) return;
const { expires_at, refresh_token } = fissa.by.accounts[0];

if (!refresh_token) return;

Expand All @@ -127,7 +115,7 @@ export class AuthService extends ServiceWithContext {
return this.refreshToken(refresh_token);
};

private createUser = async (
private createAccount = async (
spotifyUser: SpotifyApi.CurrentUsersProfileResponse,
tokens: Awaited<ReturnType<SpotifyService["codeGrant"]>>,
) => {
Expand Down
Loading

1 comment on commit fc6cd39

@vercel
Copy link

@vercel vercel bot commented on fc6cd39 Jan 4, 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.