Skip to content

Instantly share code, notes, and snippets.

@igeligel
Created June 24, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igeligel/c71eca7392042db405231f2627f0d753 to your computer and use it in GitHub Desktop.
Save igeligel/c71eca7392042db405231f2627f0d753 to your computer and use it in GitHub Desktop.
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class BetaEmail {
@PrimaryGeneratedColumn()
id: number;
@Column()
email: string;
@Column({ default: false })
loginAllowed: Boolean;
@Column({ nullable: true })
githubUsername: string;
@Column({ nullable: true })
googleEmail: string;
}
import { getRepository } from "typeorm";
import { BetaEmail } from "../entity/BetaEmail";
import { EmailReminderSetting } from "../entity/EmailReminderSetting";
import { User } from "../entity/User";
export const setupEmailRemindersRoutes = ({ server, connection }: any) => {
server.get("/api/profile/email-reminders", async (req: any, res: any) => {
if (!req.isAuthenticated()) {
return res.json({
status: "NOT_AUTHORIZED",
messages: ["The user is not logged in or cannot create a new template"],
});
}
const userRepository = getRepository(User);
const user = await userRepository.findOne({
where: { id: req.user.id },
relations: ["emailReminderSetting"],
});
return res.status(200).json(user.emailReminderSetting);
});
server.post("/api/profile/email-reminders", async (req: any, res: any) => {
if (!req.isAuthenticated()) {
return res.json({
status: "NOT_AUTHORIZED",
messages: ["The user is not logged in or cannot create a new template"],
});
}
const userRepository = getRepository(User);
const emailReminderSettingRepository = getRepository(EmailReminderSetting);
const user = await userRepository.findOne({
where: { id: req.user.id },
relations: ["emailReminderSetting"],
});
if (user.emailReminderSetting) {
// Update
const emailReminderToUpdate = await emailReminderSettingRepository.findOne(
{
where: { id: user.emailReminderSetting.id },
},
);
emailReminderToUpdate.active = req.body.active;
emailReminderToUpdate.time = req.body.time;
emailReminderToUpdate.email = req.body.email;
await emailReminderSettingRepository.save(emailReminderToUpdate);
return res.status(200).json(emailReminderToUpdate);
} else {
// Create
const emailReminderToCreate = new EmailReminderSetting();
emailReminderToCreate.active = req.body.active;
emailReminderToCreate.time = req.body.time;
emailReminderToCreate.email = req.body.email;
const result = await emailReminderSettingRepository.save(
emailReminderToCreate,
);
user.emailReminderSetting = result;
userRepository.save(user);
return res.status(201).json(emailReminderToCreate);
}
});
};
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class EmailReminderSetting {
@PrimaryGeneratedColumn()
id: number;
@Column({ default: false, nullable: false })
active: Boolean;
@Column({ type: "time with time zone", nullable: true })
time: string;
@Column({ nullable: true })
email: string;
}
import {
Entity,
PrimaryGeneratedColumn,
OneToMany,
OneToOne,
JoinColumn,
Column,
Generated,
} from "typeorm";
import { v4 as uuidv4 } from "uuid";
import { UserEmail } from "./UserEmail";
import { GithubProfile } from "./GithubProfile";
import { GrowthPlan } from "./GrowthPlan";
import { EmailReminderSetting } from "./EmailReminderSetting";
import { GoogleProfile } from "./GoogleProfile";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@OneToMany(
_type => UserEmail,
userEmail => userEmail.user,
)
userEmails: UserEmail[];
@OneToOne(type => GithubProfile)
@JoinColumn()
githubProfile: GithubProfile;
@OneToOne(type => EmailReminderSetting)
@JoinColumn()
emailReminderSetting: EmailReminderSetting;
@OneToMany(
_type => GrowthPlan,
growthPlan => growthPlan.user,
)
growthPlans: GrowthPlan[];
@Column({ default: "DEFAULT" })
role: string;
@Column({ nullable: true })
stripeCustomerId: string;
@Column({ nullable: true })
stripeProductId: string;
@Column({ nullable: true })
stripeSubscriptionId: string;
@Column({ nullable: true })
stripeSubscriptionStatus: string;
@Column({ nullable: true, name: "stripe_current_period_end" })
stripeCurrentPeriodEnd: Date;
@Column({ nullable: true })
@Generated("uuid")
unsubscribeUuid: string;
@OneToOne(type => GoogleProfile)
@JoinColumn()
googleProfile: GoogleProfile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment