Skip to content

Commit

Permalink
Update stream mappings on an attempt to verify a different stream for…
Browse files Browse the repository at this point in the history
… the same track
  • Loading branch information
nukeop committed Mar 20, 2023
1 parent 44327d7 commit 3bbdfa0
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 5 deletions.
35 changes: 34 additions & 1 deletion src/stream-mappings/stream-mappings.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ describe('Stream mappings - E2E tests', () => {
});
});

it('should verify a track', async () => {
it('should verify a new track', async () => {
const timestamp = new Date().toISOString();
SupabaseMock.streamMappings.findByTrackAndAuthor404(
'Test Sabbath',
'Test Pigs',
'Youtube',
'test-author-id',
);
SupabaseMock.streamMappings.post(
new StreamMappingBuilder().build(),
'test-id',
Expand All @@ -139,6 +145,33 @@ describe('Stream mappings - E2E tests', () => {
.expect(201);
});

it('should update the existing verification if the user verifies another stream for the same track', async () => {
const timestamp = new Date().toISOString();
SupabaseMock.streamMappings.findByTrackAndAuthor(
new StreamMappingBuilder().build(),
'Test Sabbath',
'Test Pigs',
'Youtube',
'test-author-id',
);
SupabaseMock.streamMappings.updateById(
new StreamMappingBuilder().withStreamId('another-stream-id').build(),
'test-id',
timestamp,
);

await request(app.getHttpServer())
.post('/stream-mappings/verify')
.send({
author_id: 'test-author-id',
artist: 'Test Sabbath',
title: 'Test Pigs',
source: 'Youtube',
stream_id: 'another-stream-id',
})
.expect(201);
});

it('should unverify a track', async () => {
SupabaseMock.streamMappings.delete(new StreamMappingBuilder().build());

Expand Down
53 changes: 49 additions & 4 deletions src/stream-mappings/stream-mappings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,33 @@ export class StreamMappingsService {

if (data) {
return data as StreamMapping[];
} else if (error) {
throw error;
} else {
return [];
}
}

private async findByTrackAndAuthor(
artist: string,
title: string,
source: 'Youtube',
author_id: string,
): Promise<StreamMapping | null> {
const { data, error } = await this.client
.from('stream-mappings')
.select('*')
.eq('artist', artist)
.eq('title', title)
.eq('source', source)
.eq('author_id', author_id);

if (data) {
return data[0] as StreamMapping;
} else if (error) {
throw error;
} else {
return null;
}
}

Expand Down Expand Up @@ -79,6 +104,7 @@ export class StreamMappingsService {
const verifiedByCurrentUser = streamMappings.find(
(streamMapping) => streamMapping.author_id === author_id,
);

if (verifiedByCurrentUser) {
return {
...(streamIdsWithScores.find(
Expand All @@ -95,10 +121,29 @@ export class StreamMappingsService {
async verifyTrack(
createStreamMappingDto: CreateStreamMappingDto,
): Promise<void> {
const { error } = await this.client
.from('stream-mappings')
.insert([createStreamMappingDto])
.single();
const existingStreamMapping = await this.findByTrackAndAuthor(
createStreamMappingDto.artist,
createStreamMappingDto.title,
createStreamMappingDto.source,
createStreamMappingDto.author_id,
);

let error;
if (existingStreamMapping) {
const result = await this.client
.from('stream-mappings')
.update({
stream_id: createStreamMappingDto.stream_id,
})
.eq('id', existingStreamMapping.id);
error = result.error;
} else {
const result = await this.client
.from('stream-mappings')
.insert([createStreamMappingDto])
.single();
error = result.error;
}

if (error) {
Logger.error('Error verifying track', createStreamMappingDto, error);
Expand Down
52 changes: 52 additions & 0 deletions test/supabase-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ export const SupabaseMock = {
source: `eq.${source}`,
})
.reply(200, streamMappings),
findByTrackAndAuthor: (
streamMapping: StreamMapping | null,
artist: string,
title: string,
source: StreamMapping['source'],
author_id: string,
) =>
nock(SupabaseUrl)
.get('/rest/v1/stream-mappings')
.query({
select: '*',
artist: `eq.${artist}`,
title: `eq.${title}`,
source: `eq.${source}`,
author_id: `eq.${author_id}`,
})
.reply(200, streamMapping ? [streamMapping] : []),
findByTrackAndAuthor404: (
artist: string,
title: string,
source: StreamMapping['source'],
author_id: string,
) =>
nock(SupabaseUrl)
.get('/rest/v1/stream-mappings')
.query({
select: '*',
artist: `eq.${artist}`,
title: `eq.${title}`,
source: `eq.${source}`,
author_id: `eq.${author_id}`,
})
.reply(404),
post: (
createStreamMappingDto: CreateStreamMappingDto,
id: string,
Expand All @@ -85,6 +118,25 @@ export const SupabaseMock = {
created_at,
},
]),
updateById: (
createStreamMappingDto: CreateStreamMappingDto,
id: string,
created_at: string,
) =>
nock(SupabaseUrl)
.patch('/rest/v1/stream-mappings', (body) =>
matches(body[0])(createStreamMappingDto),
)
.query({
id: `eq.${id}`,
})
.reply(200, [
{
...createStreamMappingDto,
id,
created_at,
},
]),
delete: (deleteStreamMappingDto: DeleteStreamMappingDto) =>
nock(SupabaseUrl)
.delete('/rest/v1/stream-mappings', (body) =>
Expand Down

0 comments on commit 3bbdfa0

Please sign in to comment.