Last active
June 29, 2021 04:43
-
-
Save ktutnik/cdea1cd17645b6527b16435d2a7ee656 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@genericController(c => { | |
// Only Shop Owner can modify and delete the shop data | |
c.methods("Delete", "Patch", "Put").authorize("ShopOwner") | |
}) | |
@Entity() | |
export class Shop extends EntityBase { | |
@val.required() | |
@Column() | |
name: string | |
// restrict anyone to read/write users relation | |
@authorize.none() | |
@OneToMany(() => User, x => x.shop) | |
users: User[] | |
// this method executed before entity saved to database | |
@preSave("post") | |
async setShopOwner(@bind.user() user: JwtClaims) { | |
// User who created the shop assigned as the Shop Owner by default | |
const owner = await getRepository(ShopUser) | |
.save({ user: { id: user.userId }, role: "ShopOwner" }) | |
this.users = [owner] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment