Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions apps/spin-cycle/src/mailer/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as process from 'node:process';
export class MailerService {
private readonly logger: Logger = new Logger(MailerService.name);

sendRecommendationMail(spin: SpinEntity, user: UserEntity): Promise<MessagesSendResult> {
return this.sendMail([user.email], this.getRecommendationSubject(), this.getRecommendationBody(spin));
sendRecommendationMail(spin: SpinEntity, user: UserEntity, dateAdded?: string): Promise<MessagesSendResult> {
return this.sendMail([user.email], this.getRecommendationSubject(), this.getRecommendationBody(spin, dateAdded));
}

sendAdminSignupMail(user: UserEntity): Promise<MessagesSendResult> {
Expand Down Expand Up @@ -38,16 +38,31 @@ export class MailerService {
return `Your ${month} ${day} record-mendation from SpinCycle has arrived! 😤`;
}

private getRecommendationBody(spin: SpinEntity): string {
private getRecommendationBody(spin: SpinEntity, dateAdded?: string): string {
const addedLine = dateAdded
? `<p>This release was added to your collection on <strong>${this.formatDate(dateAdded)}</strong></p>`
: '';

return `
<p>Hello there!</p>
<p>Today's randomly selected record from your collection is:</p>
<p><strong>${spin.recordName}</strong> by <strong>${spin.artistName}</strong></p>
${addedLine}
<p><a href="https://discogs.com/release/${spin.discogsId}">View record on Discogs</a></p>
<p>Happy spinning!</p>
`;
}

private formatDate(iso: string): string {
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return iso;
return d.toLocaleDateString(undefined, {
year: 'numeric',
month: 'short',
day: 'numeric',
});
}

private getClient(): IMailgunClient {
return new Mailgun(FormData).client({
username: 'api',
Expand Down
4 changes: 2 additions & 2 deletions apps/spin-cycle/src/worker/worker.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export class WorkerService {
return this.markUserAsAllPlayed(user);
}

const { discogsId, artistName, recordName } = nextSpin;
const { discogsId, artistName, recordName, dateAdded } = nextSpin;
const releaseSpin: SpinEntity = new SpinEntity(null, user, discogsId, artistName, recordName, new Date());
await Promise.all([
this.spinsService.create(releaseSpin),
this.mailerService.sendRecommendationMail(releaseSpin, user),
this.mailerService.sendRecommendationMail(releaseSpin, user, dateAdded),
]);
}

Expand Down
2 changes: 2 additions & 0 deletions libs/shared/src/lib/discogs-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface IDiscogsReleases extends IDiscogsResponse {

export interface IDiscogsRelease {
id: number;
// When the user added this release to their collection
date_added?: string;
basic_information: {
title: string;
resource_url: string;
Expand Down
7 changes: 5 additions & 2 deletions libs/shared/src/lib/dto/release-out.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ export class ReleaseOut {
discogsId: number;
artistName: string;
recordName: string;
// ISO string from Discogs indicating when the item was added to the user's collection
dateAdded?: string;

constructor(discogsId: number, artistName: string, recordName: string) {
constructor(discogsId: number, artistName: string, recordName: string, dateAdded?: string) {
this.discogsId = discogsId;
this.artistName = artistName;
this.recordName = recordName;
this.dateAdded = dateAdded;
}

static fromDiscogsResponse(release: IDiscogsRelease): ReleaseOut {
const joinedArtists: string = release.basic_information.artists.map((artist) => artist.name).join(', ');
return new ReleaseOut(release.id, joinedArtists, release.basic_information.title);
return new ReleaseOut(release.id, joinedArtists, release.basic_information.title, release.date_added);
}
}