Skip to content

Commit

Permalink
Inclusão do contador de uso das Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Sep 28, 2022
1 parent 8eb2939 commit 1178a20
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 36 deletions.
9 changes: 1 addition & 8 deletions backend/src/models/ContactTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
CreatedAt,
UpdatedAt,
Model,
ForeignKey,
BelongsTo
ForeignKey
} from "sequelize-typescript";
import Tag from "./Tag";
import Contact from "./Contact";
Expand All @@ -27,12 +26,6 @@ class ContactTag extends Model<ContactTag> {

@UpdatedAt
updatedAt: Date;

@BelongsTo(() => Contact)
contact: Contact;

@BelongsTo(() => Tag)
tag: Tag;
}

export default ContactTag;
12 changes: 8 additions & 4 deletions backend/src/models/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
Model,
PrimaryKey,
AutoIncrement,
BelongsToMany
BelongsToMany,
HasMany
} from "sequelize-typescript";
import Contact from "./Contact";
import ContactTag from "./ContactTag";
Expand All @@ -24,14 +25,17 @@ class Tag extends Model<Tag> {
@Column
color: string;

@BelongsToMany(() => Contact, () => ContactTag)
contacts: Contact[];

@CreatedAt
createdAt: Date;

@UpdatedAt
updatedAt: Date;

@BelongsToMany(() => Contact, () => ContactTag)
contacts: Array<Contact & { ContactTag: ContactTag }>;

@HasMany(() => ContactTag)
contacttag: ContactTag[];
}

export default Tag;
27 changes: 4 additions & 23 deletions backend/src/services/TagServices/ListService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Op, Sequelize } from "sequelize";
import { Op } from "sequelize";
import Tag from "../../models/Tag";
import Contact from "../../models/Contact";
import ContactTag from "../../models/ContactTag";

interface Request {
searchParam?: string;
Expand Down Expand Up @@ -35,28 +35,9 @@ const ListService = async ({
limit,
offset,
order: [["name", "ASC"]],
include: [
{
model: Contact,
as: "contacts",
attributes: [],
required: false
}
],
attributes: {
include: [
[Sequelize.fn("COUNT", Sequelize.col("contacts.id")), "contactsCount"]
]
},
group: [
"Tag.id",
"contacts.ContactTag.tagId",
"contacts.ContactTag.contactId",
"contacts.ContactTag.createdAt",
"contacts.ContactTag.updatedAt"
],
subQuery: false
include: [{ model: ContactTag, attributes: ["tagId"] }]
});

const hasMore = count > offset + tags.length;

return {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/pages/Tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ const Tags = () => {
<TableRow>
<TableCell align="center">{i18n.t("tags.table.name")}</TableCell>
<TableCell align="center">{i18n.t("tags.table.color")}</TableCell>
<TableCell align="center">{i18n.t("tags.table.contacts")}</TableCell>
<TableCell align="center">{i18n.t("tags.table.actions")}</TableCell>
</TableRow>
</TableHead>
Expand All @@ -314,6 +315,7 @@ const Tags = () => {
/>
</div>
</TableCell>
<TableCell align="center">{tag.contacttag.length ? (<span>{tag.contacttag.length}</span>) : <span>0</span>}</TableCell>
<TableCell align="center">
<IconButton
size="small"
Expand All @@ -334,7 +336,7 @@ const Tags = () => {
</TableCell>
</TableRow>
))}
{loading && <TableRowSkeleton columns={3} />}
{loading && <TableRowSkeleton columns={4} />}
</>
</TableBody>
</Table>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/translate/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ const messages = {
table: {
name: "Tags",
color: "Color",
contacts: "Contacts",
actions: "Action"
},
toasts: {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/translate/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ const messages = {
table: {
name: "Etiquetas",
color: "Color",
contacts: "Contactos",
actions: "Acción"
},
toasts: {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/translate/languages/pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ const messages = {
table: {
name: "Tags",
color: "Cor",
contacts: "Contatos",
actions: "Ação"
},
toasts: {
Expand Down

0 comments on commit 1178a20

Please sign in to comment.